Description:
When using MySQL Connector/J to perform a batch update via Statement.executeBatch(), a subsequent call to Statement.getUpdateCount() returns the update count for the last batch statement, and MYSQL outputs 0 instead of -1 as per the JDBC specification to indicate "no singleresult update count" according to the JDBC specification. This is different from both the JDBC standard and the behavior of other JDBC drivers (e.g., OceanBase Connector/J, MariaDB Connector/J) that return -1 in this case.
JDK documentation link:https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#getUpdateCount--
According to the JDK documentation: "the current result as an update count; -1 if the current result is a ResultSet object or there are no more results."
The result of this output should therefore be -1, since neither DESC nor TRUNCATE affects the number of rows. I tested it on other connectors found that the output was also -1.
How to repeat:
import java.sql.*;
public class test {
public static void main(String[] args) throws SQLException {
String url1 = "jdbc:mysql://localhost:3306/test?user=root&password=1234";
String url2 = "jdbc:mariadb://localhost:3306/test?user=root&password=1234";
Connection con = null;
Statement stmt = null;
con = DriverManager.getConnection(url1);
stmt = con.createStatement();
stmt.executeUpdate("DROP TABLE IF EXISTS table0;");
stmt.executeUpdate("CREATE TABLE table0(id INT PRIMARY KEY);");
stmt.addBatch("DESC table0");
stmt.addBatch("TRUNCATE table table0");
stmt.executeBatch();
System.out.println(stmt.getUpdateCount());
}
}