使用groovy Sql批量插入?
如何在模拟准备好的语句时使用 groovy Sql 进行批量插入?我发现的所有示例都与以下类似,并且不使用准备好的语句。
withBatch { stmt ->
stmt.addBatch("insert into table (field1,field2) values('value1','value2')")
stmt.addBatch("insert into table (field1,field2) values('value3','value4')")
}
How can you do a batch insert using groovy Sql while simulating prepared statements? All the examples I've found are similar to the following and don't use prepared statements.
withBatch { stmt ->
stmt.addBatch("insert into table (field1,field2) values('value1','value2')")
stmt.addBatch("insert into table (field1,field2) values('value3','value4')")
}
According to this link https://issues.apache.org/jira/browse/GROOVY-3504 there is no way to use prepared statements directly from within batch. What is the best way to simulate this so I can avoid having to write my own code to avoid sql injection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Groovy 1.8.1 引入了对带有批处理的准备好的语句的支持。简单的例子:
另请参阅我关于该主题的帖子: http: //novyden.blogspot.com/2011/09/groovy-batch-prepared-statement-nice.html
Groovy 1.8.1 introduced support for prepared statements with batching. Simple example:
Also see my post on the topic: http://novyden.blogspot.com/2011/09/groovy-batch-prepared-statement-nice.html
从 1.8.1 版本开始支持。您可以阅读 Groovy 1.8.1 发行说明细节。
请查看API文档寻求帮助。
It's supported from version 1.8.1. You can read the Groovy 1.8.1 release notes for details.
Pls check the API Document for help.
相关的还有 https://issues.apache.org/jira/browse/GROOVY-4328 。
从上面的 JIRA 中:
在模拟准备好的语句方面, ,请参阅Java - escape字符串以防止 SQL 注入
也就是说,您可以应用上面的启发式方法并装饰
withBatch
方法Also related is https://issues.apache.org/jira/browse/GROOVY-4328.
From the above JIRA:
In terms of simulating prepared statements, see Java - escape string to prevent SQL injection
That being said, you could apply a heuristic from the above and decorate the
withBatch
methodOwasp ESAPI。
https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API
当准备好的语句和存储过程不是一个选项时,您唯一的选择是手动转义用户输入。
ESAPI 具有可用于生产的参考方法。
来源:https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#Databas
Owasp ESAPI.
https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API
Your only option when prepared statements and stored procs are NOT an option, is to manually escape user input.
ESAPI has working, production ready reference methods.
Source: https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#Databas