首页 > 解决方案 > 我可以将 Derby EmbeddedDriver 与 Java 教程中指定的 CachedRowSet 示例一起使用吗?

问题描述

JDBC 教程中有几个可以运行的示例 java 程序。Ant 目标 runcrs 不运行。当我在嵌入式驱动程序模式下使用 Derby 提供的代码时,出现以下错误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

在放弃并寻求解决方案之前,我查看了以下帖子。它们涵盖了非嵌入式(客户端-服务器)实现,它们没有涵盖 CachedRowSet 接口。通常,解决方案是检查 derby.jar 是否在类路径中。(我检查过——没有运气。此外,驱动程序显然正在加载,因为非 RowSet 功能正在工作,例如,找到项目 123456。

臭名昭著的 java.sql.SQLException:找不到合适的驱动程序- 涵盖了 derby 的客户端/服务器实现

SQLException: 没有为 jdbc:derby://localhost:1527 找到合适的驱动程序- 客户端-服务器,未嵌入

JDBC DERBY 找不到合适的驱动程序错误- 不是 RowSet 实例化

找不到适合 jdbc:derby://localhost:1527/prosto 的驱动程序- 客户端/服务器,未嵌入

http://apache-database.10148.n7.nabble.com/No-suitable-driver-found-for-jdbc-derby-td108280.html - 使用Class.forName;没有行集

JDBC 嵌入式 Derby:找不到合适的驱动程序- 根本原因:连接字符串上的语法错误

java.sql.SQLException:找不到适合 jdbc:derby 的驱动程序: -根本原因:类路径

标签: javajdbcderby

解决方案


事实证明,这些教程最近可能没有使用 Derby 的 EmbeddedDriver 进行测试。通过将连接对象传递给不同的execute方法重载,代码将毫无例外地执行:

...

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

  ...

推荐阅读