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();
}