简体   繁体   English

在SQL Server 2014上运行复合查询不会返回结果集

[英]Running composite query on SQL server 2014 does not return result set

We have the following code: 我们有以下代码:

    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();
    }

This was running fine on SQL server 2005. Recently we upgraded to SQL server 2014. I have also updated the jar to SQLJDBC4.jar (as we are using JDK6 as runtime). 这在SQL Server 2005上运行良好。最近我们升级到了SQL Server2014。我也将jar更新为SQLJDBC4.jar(因为我们使用JDK6作为运行时)。 But running this on SQL server 2014 leads to following exception. 但是在SQL Server 2014上运行此命令会导致以下异常。

Exception: 例外:

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)

This maybe because 2012 returns update count -> result set, where 2005 returned result set -> update count, but that is just speculation (any confirmation here will be added bonus for me). 这可能是因为2012年返回更新计数->结果集,而2005年返回更新结果->结果集,但这仅仅是猜测(此处的任何确认都会为我增加奖金)。

I don't want to change the executeQuery to execute/executeUpdate. 我不想将executeQuery更改为execute / executeUpdate。 Is there any other way of getting around this exception? 还有其他解决此异常的方法吗? Also i don't use stored procedures. 另外我不使用存储过程。

Or 要么

Is there any other SQL driver that i can use and make the composite query work on sql server 2014 是否有我可以使用的其他SQL驱动程序并使复合查询在sql server 2014上工作

is your table name 您的表格名称是

Table1\\n 表1 \\ n

if that's not your table name the remove \\n from the query 如果不是您的表名,则从查询中删除\\ n

I just changed the driver to JTD as follows and it worked like a charm 我只是将驱动程序更改为JTD,如下所示,它像一个魅力一样工作

Connection conn = null; 连接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();
    }

OUTPUT: OUTPUT:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM