插入到数据库我有一个数据库表GroovyTest如下: -无法从骡子的Groovy
正如你可以看到它有4个领域: - ID,NAME, AGE和DESIGNATION其中ID和AGE是整数类型和剩余的NAME和DESIGNATION是字符串
现在我正试图使用Groovy脚本这个数据库插入值以下骡子流量: -
<flow name="GroovyWithJDBCFlow1" doc:name="GroovyWithJDBCFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" path="groovy" doc:name="HTTP"/>
<scripting:component doc:name="Initialise Database">
<scripting:script engine="Groovy">
<scripting:text><![CDATA[
jdbcConnector = muleContext.getRegistry().lookupConnector("Database_Global");
qr = jdbcConnector.getQueryRunner();
conn = jdbcConnector.getConnection();
qr.update(conn, "INSERT INTO GroovyTest VALUES(message.getInboundProperty('id'),message.getInboundProperty('name'),message.getInboundProperty('age'),message.getInboundProperty('designation'))")
return "Inserted into Table";]]></scripting:text>
</scripting:script>
</scripting:component>
</flow>
现在,当我触发与以下流网址: - http://localhost:8082/groovy/?method=insert&id=1&name=Anirban&age=29&designation=SoftwareEngineer
我得到以下异常: -
Root Exception stack trace:
com.microsoft.sqlserver.jdbc.SQLServerException: Cannot find either column "message" or the user-defined function or aggregate "message.getInboundProperty", or the name is ambiguous.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1454)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
但是,如果我硬编码值如下: - qr.update(conn, "INSERT INTO GroovyTest VALUES(5,'testvalue',333,'test')");
它变得不插入任何问题..
而且我查了日志..我在入境的属性值正确地得到,但我不能让它插入DB使用Groovy
请帮
在Groovy脚本中直接与数据库连接器交互也是一个糟糕的主意。尝试使用[数据库连接器](http://www.mulesoft.org/documentation/display/current/Database+Connector)执行此操作。您可以在查询中使用MEL,并且数据库连接器将自动使用参数化的PreparedStatement来运行它。 – 2014-11-09 19:32:33
我已经看到OP使用DB连接器的其他帖子,所以我认为他知道这个连接器,并希望探索替代方法。正如你所说,当有一个可用的时候绕过运输通常是一个坏主意,因为你失去了Mule提供的额外功能(错误处理,统计数据等)。 – 2014-11-09 21:59:38