使用groovy Sql批量插入?

发布于 2024-09-01 15:34:01 字数 487 浏览 8 评论 0原文

如何在模拟准备好的语句时使用 groovy Sql 进行批量插入?我发现的所有示例都与以下类似,并且不使用准备好的语句。

withBatch  { stmt ->
stmt.addBatch("insert into table (field1,field2) values('value1','value2')")
stmt.addBatch("insert into table (field1,field2) values('value3','value4')")
}

根据此链接 https://issues.apache.org/jira/browse/GROOVY-第3504章 没有办法直接从批处理中使用准备好的语句。模拟这个的最佳方法是什么,这样我就可以避免编写自己的代码来避免 SQL 注入?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风渺 2024-09-08 15:34:01

Groovy 1.8.1 引入了对带有批处理的准备好的语句的支持。简单的例子:

sql.withBatch(20, """update some_table 
                        set some_column = :newvalue 
                      where id = :key """) { ps ->                 
          mymap.each { k,v ->
              ps.addBatch(key:k, newvalue:v)
          }
}

另请参阅我关于该主题的帖子: 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:

sql.withBatch(20, """update some_table 
                        set some_column = :newvalue 
                      where id = :key """) { ps ->                 
          mymap.each { k,v ->
              ps.addBatch(key:k, newvalue:v)
          }
}

Also see my post on the topic: http://novyden.blogspot.com/2011/09/groovy-batch-prepared-statement-nice.html

小伙你站住 2024-09-08 15:34:01

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

迟到的我 2024-09-08 15:34:01

相关的还有 https://issues.apache.org/jira/browse/GROOVY-4328

从上面的 JIRA 中:

...我们能(简单地)做的最好的事情就是
将这样的 GString 转换为普通的
细绳。为此我们可以做一点
比我们目前所做的多一点
解析字符串并尝试引用或
转义“字符串”看起来的东西但不是
数字或日期看起来的东西但它
可能不是很优雅。通常我们
会使用“?”特点
占位符和准备好的声明
并且要做的事情会少得多

在模拟准备好的语句方面, ,请参阅Java - escape字符串以防止 SQL 注入

也就是说,您可以应用上面的启发式方法并装饰 withBatch 方法

Also related is https://issues.apache.org/jira/browse/GROOVY-4328.

From the above JIRA:

...the best we could (simply) do is
convert such a GString to a normal
String. For that we could do a little
bit more than we currently do by
parsing the String and try to quote or
escape "string" looking things but not
number or date looking things but it
might not be very elegant. Normally we
would use the "?" character
placeholders and a prepared statement
and would have much less to do

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 method

梦回旧景 2024-09-08 15:34:01

Owasp ESAPI。
https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API

当准备好的语句和存储过程不是一个选项时,您唯一的选择是手动转义用户输入。

ESAPI 具有可用于生产的参考方法。

Codec ORACLE_CODEC = new OracleCodec();
 String query = "SELECT user_id FROM user_data WHERE user_name = '" + 
   ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter("userID")) + "' and user_password = '"
   + ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter("pwd")) +"'";

来源: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.

Codec ORACLE_CODEC = new OracleCodec();
 String query = "SELECT user_id FROM user_data WHERE user_name = '" + 
   ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter("userID")) + "' and user_password = '"
   + ESAPI.encoder().encodeForSQL( ORACLE_CODEC, req.getParameter("pwd")) +"'";

Source: https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet#Databas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文