如何预处理语句与Apache DBUtils使用吗?准备好的语句如何与Apache DBUtils一起使用?
似乎大多数为org.apache.commons.dbutils的方法。*期望字符串参数。令人惊讶的是没有接受PreparedStatements的方法。
如何预处理语句与Apache DBUtils使用吗?准备好的语句如何与Apache DBUtils一起使用?
似乎大多数为org.apache.commons.dbutils的方法。*期望字符串参数。令人惊讶的是没有接受PreparedStatements的方法。
// Execute the query and get the results back from the handler
Object[] result = run.query(
"SELECT * FROM Person WHERE name=?", h, "John Doe");
这表明存在必须使用一个PreparedStatement。并在the source for the query method我们看到
private <T> T query(Connection conn, boolean closeConn, String sql,
ResultSetHandler<T> rsh, Object... params)
...
PreparedStatement stmt = null;
ResultSet rs = null;
T result = null;
try {
stmt = this.prepareStatement(conn, sql);
this.fillStatement(stmt, params);
rs = this.wrap(stmt.executeQuery());
result = rsh.handle(rs);
} catch (SQLException e) {
...
结论?正在使用PreparedStatement
,根本不需要担心。
得到它的工作。感谢您指出正确的方向。 – VisWebsoft 2013-04-13 04:03:33
预处理语句用于内部DbUtils,但事先准备好的声明的一点是不要每次做一个更新时间准备的声明中,并重新使用它只是改变了PARAMS。假设你必须插入1000条记录,你想重复使用相同的准备语句只更改params。为此,请使用QueryRunner.batch而不是QueryRunner.update。
但是,如果插入了一百万条记录,我很怀疑自从QueryRunner.batch初始化准备好的语句以来,dbutils将成为解决方案。我建议编写一个自定义的实现,而不是诉诸于dbutils。我不确定是否有任何其他缓存准备好的语句的工具。 – dmachop 2016-06-28 16:13:11
看看[QueryRunner的源代码](http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/src/main/java/org/apache/commons/dbutils/QueryRunner.java ?view =标记),你会注意到它们(查看'query','update'和'insert'的主要实现)。或者你想喂这些方法现有的'PreparedStatement'实例吗? – fvu 2013-04-10 00:54:46
是的,我注意到了。这对我来说有点过分了,所以我正在努力。我以前使用Prepared Statements,但不使用这些QueryRunner方法。这就是我想要做的,所以当我处理我的表单输入时,它不会与撇号崩溃。 – VisWebsoft 2013-04-10 12:55:18