简体   繁体   中英

Can I use the Derby EmbeddedDriver with the CachedRowSet example specified in the Java tutorial?

In the JDBC Tutorial there are several example java programs that can be run. Ant target runcrs does not run. When I use the code as provided using Derby in embedded driver mode, I get an error on crs.execute :

try {
  crs.setUsername(settings.userName);
  crs.setPassword(settings.password);
  crs.setUrl(settings.urlString);
  crs.setCommand("select * from MERCH_INVENTORY");

  // Setting the page size to 4, such that we
  // get the data in chunks of 4 rows @ a time.
  crs.setPageSize(100);

  // Now get the first set of data
  crs.execute(); // Throws exception. No suitable driver found.
 [java] Found item 6914: Cookbook (12) [java] Found item 123456: TableCloth (14) [java] java.sql.SQLException: No suitable driver found for jdbc:derby:testdb [java] at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702) [java] at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228) [java] at java.sql.rowset/com.sun.rowset.internal.CachedRowSetReader.connect(CachedRowSetReader.java:340) [java] at java.sql.rowset/com.sun.rowset.internal.CachedRowSetReader.readData(CachedRowSetReader.java:157) [java] at java.sql.rowset/com.sun.rowset.CachedRowSetImpl.execute(CachedRowSetImpl.java:809) [java] at java.sql.rowset/com.sun.rowset.CachedRowSetImpl.execute(CachedRowSetImpl.java:1435) [java] at com.oracle.tutorial.jdbc.CachedRowSetSample.testPaging(CachedRowSetSample.java:98) [java] at com.oracle.tutorial.jdbc.CachedRowSetSample.main(CachedRowSetSample.java:254) [java] SQLState: 08001 [java] Error Code: 0 [java] Message: No suitable driver found for jdbc:derby:testdb

I looked at the following posts prior to giving up and seeking a solution. They covered non-embedded (client-server) implementations, they were not covering the CachedRowSet interface. Frequently the solution was to check to see if derby.jar was on the class path. (I checked -- no luck. Also, the driver was obviously loading because the non RowSet functionality was working eg, Found item 123456 .)

The infamous java.sql.SQLException: No suitable driver found - covered client/server implementations of derby

SQLException: No suitable driver found for jdbc:derby://localhost:1527 - client-server, not embedded

no suitable driver found error for JDBC DERBY - not a RowSet instantiation

No suitable driver found for jdbc:derby://localhost:1527/prosto - client/server, not embedded

http://apache-database.10148.n7.nabble.com/No-suitable-driver-found-for-jdbc-derby-td108280.html - uses Class.forName ; no RowSet

JDBC embedded Derby: No suitable driver found - root cause: syntax error on connect string

java.sql.SQLException: No suitable driver found for jdbc:derby: - root cause: class path

It turns out that the tutorials may not have been recently tested with Derby's EmbeddedDriver. By passing the connection object to a different overload of execute method, the code executes without exception:

...

public CachedRowSetSample(Connection connArg,
                        JDBCTutorialUtilities settingsArg) {
  super();
  this.con = connArg;

...

try {
  crs.setUsername(settings.userName);
  crs.setPassword(settings.password);
  crs.setUrl(settings.urlString);
  crs.setCommand("select * from MERCH_INVENTORY");

  // Setting the page size to 4, such that we
  // get the data in chunks of 4 rows @ a time.
  crs.setPageSize(100);

  // Now get the first set of data
  crs.execute(con); // Executes without error       // add 'con' (the connection object) as an arg

  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM