3

我花了很长时间阅读以前的帖子,但似乎没有一个对我有帮助。我正在尝试从 NetBeans 运行一个简单的 Java 应用程序,这里是最简单的代码:

final String DB_URL = "jdbc:derby:CoffeeDB;create=true";         
try
  {
     // Create a connection to the database.
     Connection conn = DriverManager.getConnection(DB_URL);

     // If the DB already exists, drop the tables.
     dropTables(conn);

     // Build the Coffee table.
     buildCoffeeTable(conn);

     // Close the connection.
     conn.close();
  }
  catch (Exception ex)
  {
     System.out.println("ERROR: " + ex.getMessage());
  }

通常是环境变量引起了麻烦,但我已经检查过了,它们似乎设置正确:

类路径:C:\Program Files\Java\jdk1.7.0_17\db\lib\derby.jar;C:\Program Files\Java\jdk1.7.0_17\db\lib\derbytools.jar;

DERBY_HOME C:\Program Files\Java\jdk1.7.0_17\db

PATH:(仅关于 jdbc 的部分) C:\Program Files\Java\jdk1.7.0_17\db\bin

我已经在命令提示符下使用 ij 创建了一个表并且它有效,当我尝试通过 Netbeans 运行它时,我收到此错误。不过有趣的是,当我从命令提示符运行 sysinfo 时,它会像这样出现:

------------------ Java 信息​​ ------------------

Java版本:1.7.0_17

Java 供应商:甲骨文公司

Java 主页:C:\Program Files\Java\jre7

Java 类路径:C:\Program Files\Java\jdk1.7.0_17\db\lib\derby.jar;C:\Program Files\Java\jdk1.7.0_17\db\lib\derbytools.jar;C:\Program Files \Java\jdk1.7.0_17\db/lib/derby.jar;C:\Program Files\Java\jdk1.7.0_17\db/lib/derbynet.jar;C:\Program Files\Java\jdk1.7.0_17\ db/lib/derbyclient.jar;C:\Program Files\Java\jdk1.7.0_17\db/lib/derbytools.jar

操作系统名称:Windows Vista

操作系统架构:amd64

操作系统版本:6.0

---> 如您所见,不知何故,更多信息被添加到先前的类路径中,并且 derbytools.jar 和 derby.jar 出现了两次?结合反斜杠??

通过 NetBeans 调试时,异常来自 DriverManager.java 文件中的这一行:

    Connection con = aDriver.driver.connect(url, info);     <--------------- here
    if (con != null) {
          // Success!
          println("getConnection returning " + aDriver.driver.getClass().getName());
          return (con);
    }

url 是正确的:jdbc:derby:CoffeeDB;create=true 但连接返回 null 并引发 SQLexception。有任何想法吗?

4

2 回答 2

2

异常消息“没有找到合适的驱动程序...”意味着没有加载的 JDBC 驱动程序接受 URL。这使得 JDBC 驱动程序很可能根本不包含在应用程序的类路径中

The environment variable CLASSPATH is almost always ignored when a java application is run from an IDE, and always if run from an executable jar. Make sure that the build-path in the IDE actually contains derby.jar, or if it is an executable jar, that it is listed in the META-INF/manifest.mf in the Class-Path attribute.

于 2013-03-30T07:31:14.320 回答
1

you can right click the project and do the following steps...Build -> Configure Build Path -> Libraries -> Add External Jar... now you have to specify the path to the *.jar file. simple example https://github.com/mraab89/Tutorial_Java_Database_Connectivity_JDBC

于 2014-05-29T01:21:20.213 回答