Bug #118078 A potential failure in Mysql Connector/J
Submitted: 29 Apr 5:33 Modified: 29 Apr 7:33
Reporter: 策 吕 Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.32 OS:Any
Assigned to: CPU Architecture:Any

[29 Apr 5:33] 策 吕
Description:
When I run the following code with version 8.0.32, it throws the exception "No suitable driver found for jdbc:mysql://localhost:3306/testdb0?user=root&password=1234&rewriteBatchedStatements=true".

when I use "Class.forName("com.mysql.cj.jdbc.Driver");", I still have problems.

But when I use version 8.1.0 it does not throw the above exception.Is it possible that this is a difference in driver settings between version 8.0.32 and 8.1.0?

public static void main(String[] args) throws SQLException, ClassNotFoundException {

        String url = "jdbc:mysql://localhost:3306/testdb0?user=root&password=1234&rewriteBatchedStatements=true";
        Connection con = DriverManager.getConnection(url);
        try (Statement stmt = con.createStatement()) {
            stmt.execute("DROP TABLE IF EXISTS t0");
        }
        // CREATE TABLE
        try (Statement stmt = con.createStatement()) {
            stmt.execute("CREATE TABLE t0(c0 INT UNIQUE )");
        }

        // First batch insert
        try (Statement stmt = con.createStatement()) {
            stmt.addBatch("INSERT INTO t0 VALUES (0)");
            stmt.addBatch("INSERT INTO t0 VALUES (1)");
            stmt.addBatch("INSERT INTO t0 VALUES (2)");
            stmt.executeBatch();
        }

        // Second batch: contains duplicate, truncate, and more inserts
        try (Statement stmt = con.createStatement()) {
            stmt.addBatch("INSERT INTO t0 VALUES (1)"); // duplicate
            //stmt.addBatch("DROP TABLE t0");
            stmt.addBatch("TRUNCATE t0");
            stmt.addBatch("INSERT INTO t0 VALUES (2)");
            stmt.addBatch("INSERT INTO t0 VALUES (3)");
            stmt.addBatch("INSERT INTO t0 VALUES (4)");
            stmt.executeBatch();
        }

        try (Statement stmt = con.createStatement()) {
            stmt.execute("DROP TABLE IF EXISTS t0");
        }
        // CREATE TABLE
        try (Statement stmt = con.createStatement()) {
            stmt.execute("CREATE TABLE t0(c0 INT /*PRIMARY KEY*/)");
        }

        // Query and print results
        try (Statement stmt = con.createStatement()) {
            if (stmt.execute("SELECT * FROM t0")) {
                ResultSet rs = stmt.getResultSet();
                ResultSetMetaData meta = rs.getMetaData();
                int count = meta.getColumnCount();

                while (rs.next()) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 1; i <= count; i++) {
                        sb.append("* ").append(rs.getString(i)).append(" * ");
                    }
                    System.out.println(sb.toString());
                }
                rs.close();
            } else {
                System.out.println("count: " + stmt.getUpdateCount());
            }
        }
        con.close();
    }

How to repeat:
public static void main(String[] args) throws SQLException, ClassNotFoundException {

        String url = "jdbc:mysql://localhost:3306/testdb0?user=root&password=1234&rewriteBatchedStatements=true";
        Connection con = DriverManager.getConnection(url);
        try (Statement stmt = con.createStatement()) {
            stmt.execute("DROP TABLE IF EXISTS t0");
        }
        // CREATE TABLE
        try (Statement stmt = con.createStatement()) {
            stmt.execute("CREATE TABLE t0(c0 INT UNIQUE )");
        }

        // First batch insert
        try (Statement stmt = con.createStatement()) {
            stmt.addBatch("INSERT INTO t0 VALUES (0)");
            stmt.addBatch("INSERT INTO t0 VALUES (1)");
            stmt.addBatch("INSERT INTO t0 VALUES (2)");
            stmt.executeBatch();
        }

        // Second batch: contains duplicate, truncate, and more inserts
        try (Statement stmt = con.createStatement()) {
            stmt.addBatch("INSERT INTO t0 VALUES (1)"); // duplicate
            //stmt.addBatch("DROP TABLE t0");
            stmt.addBatch("TRUNCATE t0");
            stmt.addBatch("INSERT INTO t0 VALUES (2)");
            stmt.addBatch("INSERT INTO t0 VALUES (3)");
            stmt.addBatch("INSERT INTO t0 VALUES (4)");
            stmt.executeBatch();
        }

        try (Statement stmt = con.createStatement()) {
            stmt.execute("DROP TABLE IF EXISTS t0");
        }
        // CREATE TABLE
        try (Statement stmt = con.createStatement()) {
            stmt.execute("CREATE TABLE t0(c0 INT /*PRIMARY KEY*/)");
        }

        // Query and print results
        try (Statement stmt = con.createStatement()) {
            if (stmt.execute("SELECT * FROM t0")) {
                ResultSet rs = stmt.getResultSet();
                ResultSetMetaData meta = rs.getMetaData();
                int count = meta.getColumnCount();

                while (rs.next()) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 1; i <= count; i++) {
                        sb.append("* ").append(rs.getString(i)).append(" * ");
                    }
                    System.out.println(sb.toString());
                }
                rs.close();
            } else {
                System.out.println("count: " + stmt.getUpdateCount());
            }
        }
        con.close();
    }
[29 Apr 6:54] MySQL Verification Team
Hello 策 吕,

Thank you for the report and feedback.
Please note that C/J 8.0.32 is very old and many bugs fixed since
then. May I request you to try Connector/J 9.3.0? If you are still
experiencing the issue then please provide a test case(.java), along
with exact details of MySQL Server version in use which demonstrate the
issue? Thank you.

regards,
Umesh
[29 Apr 6:58] 策 吕
Sure, you're right, this is indeed an old version, and I just overlooked the problem, thanks for the reply!
[29 Apr 7:33] MySQL Verification Team
Thank you, closing it for now.

regards,
Umesh