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

岁酱吖の 提交于 2019-12-12 01:55:52

问题


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). But running this on SQL server 2014 leads to following exception.

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).

I don't want to change the executeQuery to 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


回答1:


is your table name

Table1\n

if that's not your table name the remove \n from the query




回答2:


I just changed the driver to JTD as follows and it worked like a charm

Connection 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:

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


来源:https://stackoverflow.com/questions/28022701/running-composite-query-on-sql-server-2014-does-not-return-result-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!