SQL Server Mssql driver does not report error/exception when using Statement.execute() command

qf9go6mv  于 2023-08-02  发布在  其他
关注(0)|答案(3)|浏览(93)

I was trying to execute multiple lines of SQl using the Statement.execute() method like shown, and I tried to execute something that is obviously going to fail, but no exception is being thrown. Has anyone faced this issue in the past? What am I doing wrong?

Statement s = manager.createStatement();
boolean result = s.execute("CREATE TABLE table1(id int, value int, PRIMARY KEY (id));\n" +
                    "INSERT INTO table1 VALUES(1,10);\n" +
                    "INSERT INTO table1 VALUES(1,14);");

When I run this, everything looks fine, no error or exception anywhere.

I'm using the mssql-jdbc-9.2.0.jre8.jar driver. Also tried with mssql-jdbc-12.2.0.jre8.jar , no difference.

When I run the same thing using the .executeQuery() method, I get the expected exception regarding the violation of primary key.

u7up0aaq

u7up0aaq1#

If you want to execute multiple queries, you can use batch method like this:

...
            // Step 1: create statement object from connection
            Statement s = con.createStatement();
           
            // Step 2: Create a statement / create table
            String sql1
                = "CREATE TABLE STUDENT(STUDENTID VARCHAR2(10) PRIMARY KEY,NAME VARCHAR2(20),DEPARTMENT VARCHAR2(10))";
 
            // Step 3: Process a query
            // Insert records in the table
            String sql2
                = "INSERT INTO STUDENT VALUES('S101','JEAN','CSE')";
            String sql3
                = "INSERT INTO STUDENT VALUES('S102','ANA','CSE')";
            String sql4
                = "INSERT INTO STUDENT VALUES('S103','ROBERT','ECE')";
            String sql5
                = "INSERT INTO STUDENT VALUES('S104','ALEX','IT')";
            String sql6
                = "INSERT INTO STUDENT VALUES('S105','DIANA','IT')";
            s.addBatch(sql1);
            s.addBatch(sql2);
            s.addBatch(sql3);
            s.addBatch(sql4);
            s.addBatch(sql5);
            s.addBatch(sql6);
 
            // Step 4: Process the results
            // execute the sql statements
            s.executeBatch();
...
o4hqfura

o4hqfura2#

The problem is that you're executing multiple statements, and that means that the SQL server implementation will report results per statement.

You will need to use the following to get exception from the second or third statement:

boolean result = s.execute("CREATE TABLE table1(id int, value int, PRIMARY KEY (id));\n" +
                    "INSERT INTO table1 VALUES(1,10);\n" +
                    "INSERT INTO table1 VALUES(1,14);");
while (result || s.getUpdateCount() != -1) {
    result = s.getMoreResults();
}

If there is a pending exception for the second (or in this case, third) statement, the call to getMoreResults() will throw it.

Alternatively, use the JDBC API as it is intended to be used, and execute the statements individually.

ovfsdjhp

ovfsdjhp3#

I think you should print out your Boolean result for execute execute(string) returns a Boolean which I believe will most likely be false!

The executeQuery(string) must return a "ResultSet", so it would throw exception because that is not the type of statement to use in executeQuery.

Any errors of SQL code execution belong to the SQL server, the Java only is reporting the outcome by particular tools it has to check the final outcome such as error code numbers, however, some interaction through the connector is monitored and will assert error as exception as the API docs generally define will occur with particular methods.

相关问题