Description:
I use MySQL Connector J to connect to the database.
When I executed my test case, I found that when inserting duplicate data, instead of reporting a java.sql.SQLIntegrityConstraintViolationException error, it returns 1.
you can check the provided test case for detailed information.
How to repeat:
@Test
public void test() throws SQLException {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3368?user=user&password=password&sessionVariables=sql_mode=''&jdbcCompliantTruncation=false");
execute(con, "DROP DATABASE IF EXISTS test");
execute(con, "CREATE DATABASE test");
execute(con, "USE test");
execute(con, "CREATE TABLE t0(c0 BOOLEAN , c1 DATE , PRIMARY KEY(c1, c0)) engine=Aria");
batchExecute(con, new String[]{"INSERT INTO t0 VALUES (1795252662, '2023-03-13')"});
// this one throw error
execute(con, "INSERT INTO t0 VALUES (1795252662, '2023-03-13')");
batchExecute(con, new String[]{"TRUNCATE t0"});
Statement statement = con.createStatement();
int res = statement.executeUpdate("INSERT INTO t0 VALUES (1795252662, '2023-03-13')");
// why return 1?
System.out.println(res);
}
public void execute(Connection con, String sql) {
try {
Statement statement = con.createStatement();
statement.execute(sql);
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void batchExecute(Connection con, String[] sql) {
try {
Statement statement = con.createStatement();
for (String s : sql) {
statement.addBatch(s);
}
statement.executeBatch();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Suggested fix:
it should throw java.sql.SQLIntegrityConstraintViolationException instead of returning 1, which is a misleading value.