运行 SQL 服务器 2014 上的复合查询没有 return 结果集
Running composite query on SQL server 2014 does not return result set
我们有以下代码:
Connection conn = null;
String dbURL = "jdbc:sqlserver://DBDerver details here";
String user = "user name";
String pass = "password@123";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(dbURL, user, pass);
String sql = "update Table1" + "set DBID = DBID+1 where TABLENAME = '" + "Table2" + "';" + "select DBID from Table 1 where TABLENAME = '" + "Table 2" + "'";
System.out.println("generateId(), SQL = " + sql);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int id = -1;
System.out.println("Result set :->"+rs);
while(rs.next()) {
id = rs.getInt(1);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这在 SQL 服务器 2005 上 运行 很好。最近我们升级到 SQL 服务器 2014。我也将 jar 更新到 SQLJDBC4.jar(因为我们是使用 JDK6 作为运行时)。但是 运行 这在 SQL 服务器 2014 上导致以下异常。
异常:
com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:800)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:689)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:616)
at com.hcl.JDBCTest$QueryTogether.executeQueryHere(JDBCTest.java:63)
at com.hcl.JDBCTest.main(JDBCTest.java:34)
这可能是因为 2012 returns 更新计数 -> 结果集,其中 2005 返回结果集 -> 更新计数,但这只是猜测(这里的任何确认都会为我增加奖励)。
我不想将 executeQuery 更改为 execute/executeUpdate。还有其他方法可以解决此异常吗?我也不使用存储过程。
或
是否有任何其他 SQL 驱动程序我可以使用并使复合查询在 sql 服务器 2014
上运行
是你的 table 名字
Table1\n
如果这不是您的 table 名称,请从查询中删除 \n
我刚刚将驱动程序更改为 JTD,效果非常好
连接 conn = null;
String dbURL = "jdbc:jtds:sqlserver://DataBase Name";
String user = "username";
String pass = "password";
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection(dbURL, user, pass);
JDBCTest jt = new JDBCTest();
String sql = "update Table1 " + "set DBID = DBID+1 where TABLENAME = '" + "Table2'" +"\n"+
"select DBID from Table1 where TABLENAME = '" + "Table2" + "'";
System.out.println("generateId(), SQL = " + sql);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int id = -1;
System.out.println("Result set :->"+rs);
while(rs.next()) {
id = rs.getInt(1);
System.out.println("id is :-> "+ id);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
输出:
generateId(), SQL = update SEQUENCE_TABLE set DBID = DBID+1 where TABLENAME = 'TBGP_TABLE'
select DBID from SEQUENCE_TABLE where TABLENAME = 'TBGP_TABLE'
Result set :->net.sourceforge.jtds.jdbc.JtdsResultSet@d42d08
id is :-> 86532
我们有以下代码:
Connection conn = null;
String dbURL = "jdbc:sqlserver://DBDerver details here";
String user = "user name";
String pass = "password@123";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(dbURL, user, pass);
String sql = "update Table1" + "set DBID = DBID+1 where TABLENAME = '" + "Table2" + "';" + "select DBID from Table 1 where TABLENAME = '" + "Table 2" + "'";
System.out.println("generateId(), SQL = " + sql);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int id = -1;
System.out.println("Result set :->"+rs);
while(rs.next()) {
id = rs.getInt(1);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这在 SQL 服务器 2005 上 运行 很好。最近我们升级到 SQL 服务器 2014。我也将 jar 更新到 SQLJDBC4.jar(因为我们是使用 JDK6 作为运行时)。但是 运行 这在 SQL 服务器 2014 上导致以下异常。
异常:
com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:800)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:689)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:616)
at com.hcl.JDBCTest$QueryTogether.executeQueryHere(JDBCTest.java:63)
at com.hcl.JDBCTest.main(JDBCTest.java:34)
这可能是因为 2012 returns 更新计数 -> 结果集,其中 2005 返回结果集 -> 更新计数,但这只是猜测(这里的任何确认都会为我增加奖励)。
我不想将 executeQuery 更改为 execute/executeUpdate。还有其他方法可以解决此异常吗?我也不使用存储过程。
或
是否有任何其他 SQL 驱动程序我可以使用并使复合查询在 sql 服务器 2014
上运行是你的 table 名字
Table1\n
如果这不是您的 table 名称,请从查询中删除 \n
我刚刚将驱动程序更改为 JTD,效果非常好
连接 conn = null;
String dbURL = "jdbc:jtds:sqlserver://DataBase Name";
String user = "username";
String pass = "password";
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection(dbURL, user, pass);
JDBCTest jt = new JDBCTest();
String sql = "update Table1 " + "set DBID = DBID+1 where TABLENAME = '" + "Table2'" +"\n"+
"select DBID from Table1 where TABLENAME = '" + "Table2" + "'";
System.out.println("generateId(), SQL = " + sql);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int id = -1;
System.out.println("Result set :->"+rs);
while(rs.next()) {
id = rs.getInt(1);
System.out.println("id is :-> "+ id);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
输出:
generateId(), SQL = update SEQUENCE_TABLE set DBID = DBID+1 where TABLENAME = 'TBGP_TABLE'
select DBID from SEQUENCE_TABLE where TABLENAME = 'TBGP_TABLE'
Result set :->net.sourceforge.jtds.jdbc.JtdsResultSet@d42d08
id is :-> 86532