我正在尝试使用 SQL Server 数据库上的 queryrunner 接口从列表中检索多条记录。似乎在正常方法有效时界面会引发错误。
private void testMethod() throws Exception
{
List<Object> strList = new ArrayList<Object>();
strList.add("015190000000vojAAA");
strList.add("015190000000vokAAA");
strList.add("015190000000volAAA");
try (DBUtil dbUtil = new DBUtil())
{
String theQuery = "SELECT ID from Document where Id IN (?, ?, ?)";
for (Object[] row : dbUtil.queryRunner.select(theQuery, new ArrayListHandler(),
strList.toArray(new Object[strList.size()])))
{
logger.info(row[0]);
}
/*
* Normal method
* PreparedStatement prepareStatement = dbUtil.connection.prepareStatement(theQuery);
prepareStatement.setString(1, (String)strList.get(0));
prepareStatement.setString(2, (String)strList.get(1));
prepareStatement.setString(3, (String)strList.get(2));
ResultSetMetaData metaData = prepareStatement.getMetaData();
logger.info(metaData.getColumnCount());
ResultSet rs = prepareStatement.executeQuery();
while(rs.next())
{
logger.info(rs.getString(1));
}
prepareStatement.close();
*/
}
它似乎正在使用一种正常的方法,我可以单独设置参数。但是,我想使用 queryrunner 接口,因为我的项目使用此接口运行所有查询。我在这里做错什么了吗?
进一步调试 dbutils 导致错误消息 Exception in thread "main" java.sql.SQLException:
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ','. Query: SELECT ID from Document where Id IN (?, ?, ?) Parameters: [015190000000vojAAA, 015190000000vokAAA, 015190000000volAAA]
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:392)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:351)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:212)
at com.commvault.db.dao.DBUtil.select(DBUtil.java:160)
at com.commvault.test.Test.testMethod(Test.java:264)
at com.commvault.test.Test.main(Test.java:80)
与 Dbutils 相关的代码是下面的第 225 行。
/* */ public void fillStatement(PreparedStatement stmt, Object[] params)
/* */ throws SQLException
/* */ {
/* 223 */ ParameterMetaData pmd = null;
/* 224 */ if (!(this.pmdKnownBroken)) {
/* 225 */ pmd = stmt.getParameterMetaData();
/* 226 */ int stmtCount = pmd.getParameterCount();
/* 227 */ int paramsCount = (params == null) ? 0 : params.length;
/* */
/* 229 */ if (stmtCount != paramsCount) {
/* 230 */ throw new SQLException("Wrong number of parameters: expected " + stmtCount + ", was given " + paramsCount);
/* */
/* */ }
/* */
/* */ }
/* */
当我手动运行时,同一行正常工作。
编辑:DBUtil 是一个自定义包装类,它包含其他实用程序方法,并将 QueryRunner 作为私有变量。