2014-11-08 39 views
1

插入到数据库我有一个数据库表GroovyTest如下: -无法从骡子的Groovy

enter image description here

正如你可以看到它有4个领域: - IDNAMEAGEDESIGNATION其中IDAGE是整数类型和剩余的NAMEDESIGNATION是字符串

现在我正试图使用​​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

请帮

回答

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')) 
+0

在Groovy脚本中直接与数据库连接器交互也是一个糟糕的主意。尝试使用[数据库连接器](http://www.mulesoft.org/documentation/display/current/Database+Connector)执行此操作。您可以在查询中使用MEL,并且数据库连接器将自动使用参数化的PreparedStatement来运行它。 – 2014-11-09 19:32:33

+1

我已经看到OP使用DB连接器的其他帖子,所以我认为他知道这个连接器,并希望探索替代方法。正如你所说,当有一个可用的时候绕过运输通常是一个坏主意,因为你失去了Mule提供的额外功能(错误处理,统计数据等)。 – 2014-11-09 21:59:38

1

感谢David和最终工作的解决方案是: -

<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> 

是的,我与大卫和Ryan认为这是不使用Groovy中的一个有用的方法与数据库进行交互......但是,这是一种替代方法