问题
I'm trying to get acquainted with JDBC basics throuhg Oracle's "The Java™ Tutorials" and now I got stuck right here: https://docs.oracle.com/javase/tutorial/jdbc/basics/jdbcrowset.html.
For my examples, I exploit MySQL Server with the latest version of Connector/J (mysql-connector-java-8.0.16.jar). So far, the driver worked as expected. It gets connection to MySQL Server, creates database, tables, populates them with data and fills ordinary ResultSet objects with the retrieved data. However, as soon as I try to create JdbcRowSet object and perform execute(), I get SQL exception reporting: "No suitable driver found..."
So, now I'm in doubts: whether JdbcRowSet (as well as CachedRowSet / JoinRowSet / FilteredRowSet / WebRowSet) is just not implemented by Connector/J driver, or I'm doing something wrong? Or maybe this functionality is no longer supported in JDK 11?
Here is an example:
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
import java.sql.*;
import java.util.Properties;
public class JdbcRowSetTest {
public static void main(String[] args) {
String connectionURL =
"jdbc:mysql://localhost:3306/testdb?serverTimezone=Europe/Moscow";
String userName = "root";
String password = "root";
try {
Properties connectionProps = new Properties();
connectionProps.put("user", userName);
connectionProps.put("password", password);
System.out.println("Connect to database...");
Connection conn =
DriverManager.getConnection(connectionURL, connectionProps);
System.out.println("Retrieve and process data using ResultSet...");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from COFFEES");
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
float price = rs.getFloat("PRICE");
System.out.println(coffeeName + ", " + price);
}
System.out.println("Now, update the data using JdbcRowSet...");
JdbcRowSet jdbcRs = RowSetProvider.newFactory().createJdbcRowSet();
jdbcRs.setUrl(connectionURL);
jdbcRs.setUsername(userName);
jdbcRs.setPassword(password);
jdbcRs.setCommand("select * from COFFEES");
// it's where SQLException is thrown
jdbcRs.execute();
jdbcRs.absolute(2);
jdbcRs.updateFloat("PRICE", 10.99f);
jdbcRs.updateRow();
System.out.println("After updating the 2nd row:");
//... view updated table
} catch (SQLException e) {
e.printStackTrace();
}
}
}
And it's the output:
Connect to database...
Retrieve and process data using ResultSet...
Colombian, 7.99
Espresso, 9.99
Now, update the data using ResultSet...
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb?serverTimezone=Europe/Moscow
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at java.sql.rowset/com.sun.rowset.JdbcRowSetImpl.connect(JdbcRowSetImpl.java:643)
at java.sql.rowset/com.sun.rowset.JdbcRowSetImpl.prepare(JdbcRowSetImpl.java:654)
at java.sql.rowset/com.sun.rowset.JdbcRowSetImpl.execute(JdbcRowSetImpl.java:556)
at JdbcRowSetTest.main(JdbcRowSetTest.java:37)
Updated (possible solution):
As the commentators mentioned bellow, the problem described here has nothing to do with MySQL implementing rowset or not. For me, it seems to be just a bug of JDK 11.0.1, because as soon as I updated for the newer version of JDK 12.0.1, the problem dissipated, and now JdbcRowSet object is created without any MySQLException.
来源:https://stackoverflow.com/questions/56209063/does-mysql-connector-j-driver-implement-jdbcrowset-interface