sql-server – SQLServerException:执行SQL时语句未返回结果集
发布时间:2020-05-24 14:03:00 所属栏目:MsSql 来源:互联网
导读:我正在使用sqljdbc4.jar(sqljdbc_2.0)版本. 我正在执行一个插入选择返回以获得这样的身份: BEGIN INSERT INTO DateRangeOptions (Description,Code) VALUES (dateRange.quickPick.option.all,ALL); SELECT SCOPE_IDENTITY() END 我得到:
|
我正在使用sqljdbc4.jar(sqljdbc_2.0)版本. 我正在执行一个插入选择返回以获得这样的身份: BEGIN
INSERT INTO DateRangeOptions (Description,Code)
VALUES ('dateRange.quickPick.option.all','ALL');
SELECT SCOPE_IDENTITY()
END
我得到: com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set. 这条线是: st.executeQuery(updateQuery) 有任何想法吗? 解决方法从SQL 2000升级到SQL 2005并切换到Microsoft SQL Server 2005 JDBC驱动程序版本1.2.我在执行Insert后跟SELECT SCOPE_IDENTITY()时收到错误“语句没有返回结果”.我使用executeUpdate()和getGeneratedKeys而不是executeQuery解决了这个问题.这是前后代码.注意:此示例中使用的连接是java.sql.connection而不是com.microsoft.sqlserver.jdbc.SqlServerConnection. SQL 2000代码 String dbURL = "jdbc:sqlserver" + "://" + dbServer + ":" +
dbServerPort + ";SelectedMethod=cursor;databaseName="
+ dbName + ";user=xxx;password=xxx";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
java.sql.Connection connection = DriverManager.getConnection(dbURL);
sql = "insert into Contact (name) values ('ABC'); SELECT SCOPE_IDENTITY()";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
long id = rs.getLong(1);
System.out.println("Id=" + id);
}
SQL 2005代码 String dbURL = "jdbc:sqlserver" + "://" + dbServer + ":" +
dbServerPort + ";SelectedMethod=cursor;databaseName="
+ dbName + ";user=xxx;password=xxx";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
java.sql.Connection connection = DriverManager.getConnection(dbURL);
sql = "insert into Contact (name) values ('ABC'); SELECT SCOPE_IDENTITY()";
PreparedStatement ps = connection.prepareStatement(sql);
ps.executeUpdate(); // do not use execute() here otherwise you may get the error
// The statement must be executed before
// any results can be obtained on the next
// getGeneratedKeys statement.
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
long id = rs.getLong(1);
System.out.println("Id=" + id);
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
