0

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 e.g., 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

John
  • 741
  • 9
  • 18
  • What was the exception that you got? Make sure you include all the information you can about the actual exception you got, and that will help people give you the best possible suggestions. Here's some more on that: https://cwiki.apache.org/confluence/display/DERBY/UnwindExceptionChain – Bryan Pendleton Oct 22 '20 at 01:55
  • Good point. I'm almost afraid to because of the chance of a duplicate post tag. Anyway, I will add it. – John Oct 22 '20 at 14:30

1 Answers1

0

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

  ...
John
  • 741
  • 9
  • 18