在使用 ResultSet、PreparedStatement 和 Microsoft JDBC 驱动程序(版本:6.0)后,我遇到了 ResultSet 错误。错误消息是:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The cursor type/concurrency combination is not supported
我的代码如下:
public void usePreparedStatement_ResultSet_01() throws SQLException, ClassNotFoundException {
Class.forName(driver);
try(
Connection connectDB = DriverManager.getConnection(url+databaseName+user+password);
PreparedStatement pstmt = connectDB.prepareStatement("insert into person values(?,?,?)"
, ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = pstmt.executeQuery()) {
rs.next();
rs.updateInt(1, 1005);
rs.updateString(2,"boy"+3);
rs.updateInt(3, 25);
rs.updateRow();
}
}
public static void main(String [] argv) throws IOException, FileNotFoundException, SQLException, ClassNotFoundException {
useResultSet_01 insertDBbyResultSet = new useResultSet_01();
insertDBbyResultSet.useConfig("D:\\Dropbox\\coding\\practices\\JAVA8-II_JDBC\\configTest1.txt");
insertDBbyResultSet.usePreparedStatement_ResultSet_01();
}
}
我在发布这个问题之前已经阅读了文章。但我还是不明白两点:
- 为什么我使用PreparedStatement却不能使用“executeUpdate”?
- 如果我想将PreparedStatement 与ResultSet 一起使用(就像代码中所写的行一样),如何更新数据库。
非常感谢。
最佳答案
您将可更新的 ResultSet
与 SELECT
查询一起使用,而不是与 INSERT
一起使用。
你使用的一切都是错误的。只需像现在使用的那样使用 insert(无需任何特殊参数),并在调用 executeUpdate()
之前使用 setInt
等设置参数。
如果您想使用可更新的 ResultSet
,请将一些数据放入数据库中,选择它们,然后使用 rs.updateRow()
样式。
关于java - 不支持游标类型/并发组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46885201/