2

我使用 Netbeans,做一个 java 应用程序。我在 netbeans 中使用 Java DB 为 db connetion 创建了一个 ConnectDB 类。我启动了服务器,十个连接到数据库。当我运行它产生的文件时

java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver @ 25 line

java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/Libraryprj;create=true

@第 30 行代码

代码如下

package Lms;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author JOJO
 */
public class ConnectDB {
  static Connection conn;

  public static void main(String[] args) {
    String driver = "org.apache.derby.jdbc.ClientDriver";
    String connectionURL = "jdbc:derby://localhost:1527/Libraryprj;create=true";
    String createString = "CREATE TABLE Employee (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
    try {
      Class.forName(driver);
    } catch (java.lang.ClassNotFoundException e) {
      e.printStackTrace();
    }
    try {
      conn = DriverManager.getConnection(connectionURL);
      Statement stmt = (Statement) conn.createStatement();
      stmt.executeUpdate(createString);

      PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)");

      psInsert.setString(1, args[0]);
      psInsert.setString(2, args[1]);

      psInsert.executeUpdate();

      Statement stmt2 = (Statement) conn.createStatement();
      ResultSet rs = stmt2.executeQuery("select * from Employee");
      int num = 0;
      while (rs.next()) {
        System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address" + rs.getString(2));
      }
      rs.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
4

2 回答 2

5

获取这个库
http://repo.maven.apache.org/maven2/org/apache/derby/derbyclient/10.9.1.0/derbyclient-10.9.1.0.jar

并将其复制到 Derby 的 libs 文件夹中。

于 2012-07-18T13:48:28.117 回答
2

如果您使用 Tomcat,请从此处下载 derbyclient.jar 。并将 jar 文件复制到 Tomcat 的 lib 文件夹中。

于 2013-10-16T01:02:36.333 回答