我有一个数据库表 GroovyTest 如下: -
正如您所看到的,它有4个字段: - ID , NAME , AGE 和 DESIGNATION 以及它们 ID 和 AGE 是整数类型,其余 NAME 和 DESIGNATION 是字符串
现在我有以下Mule流程尝试使用Groovy脚本在此DB中插入值: -
<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')");
它的插入没有任何问题..
我也检查了日志..我正确获取所有入站属性值,但我无法使用Groovy将其插入数据库
请帮忙
答案 0 :(得分:2)
问题在于,您实际上是将message.getInboundProperty('id')
(等等)传递给SQL字符串中的数据库,这当然无法正常工作。
您需要传递值,如下所示:
"INSERT INTO GroovyTest VALUES(${message.getInboundProperty('id')}, ...
但请停在这里:这是一个非常糟糕的主意,因为它对SQL注入是开放的,因为你通过HTTP公开了它。
取而代之的是:
qr.update(conn,
'INSERT INTO GroovyTest VALUES (?,?,?,?)',
message.getInboundProperty('id').toInteger(),
message.getInboundProperty('name'),
message.getInboundProperty('age').toInteger(),
message.getInboundProperty('designation'))
答案 1 :(得分:1)
感谢大卫,最终的解决方案是: -
<scripting:component doc:name="Initialise Database">
<scripting:script engine="Groovy">
<scripting:text><![CDATA[
import org.mule.api.transport.*;// For PropertyScope.INBOUND
import org.apache.commons.dbutils.handlers.ArrayHandler;
jdbcConnector = muleContext.getRegistry().lookupConnector("Database_Global");
qr = jdbcConnector.getQueryRunner();
conn = jdbcConnector.getConnection();
log.info('NAME ' + message.getProperty('name',PropertyScope.INBOUND)) ;
qr.update(conn,'INSERT INTO getData (ID,NAME,AGE,DESIGNATION) VALUES (?,?,?,?)',(Integer) message.getInboundProperty('id').toInteger(), message.getInboundProperty('name'), (Integer) message.getInboundProperty('age').toInteger(),message.getInboundProperty('designation'));
log.info('Data inserted Successfully');
return "Data inserted Successfully";]]></scripting:text>
</scripting:script>
</scripting:component>
是的,我同意David和Ryan认为这不是一个使用Groovy与DB交互的有用方法..但是,这是另一种方法