我想连接到我计算机上的数据库。我已经创建了一个数据库,但我无法连接。我总是得到一个 classnotfoundexception。我不知道如何解决它。我的数据库名为 begindb,我想使用 org.apache.jdbc.ClientDriver 作为驱动程序。这是我程序中的代码:
private final static String JDBC_URL="jdbc:derby://localhost/begindb";
private final static String JDBC_DRIVER="org.apache.derby.jdbc.ClientDriver";
private final static String USER_ID="test";
private final static String PASSW="test";
public static void main(String[] args) {
try{
Class.forName(JDBC_DRIVER);
try(Connection conn = DriverManager.getConnection(JDBC_URL, USER_ID, PASSW)){
System.out.println("good job!!");
}
catch(SQLException e){
System.out.println("Error.");
}
}
catch(ClassNotFoundException e)
System.out.println(e.getMessage());
}
}
所以 try 语句中的第一行将不起作用。因为我收到了 classnotfoundexception。
最佳答案
来自documentation第 4 步 -
To use the Derby Network Client JDBC driver, set your CLASSPATH to include the jar files listed below:
derbyclient.jar: contains the JDBC driver
derbytools.jar: optional, provides the ij tool
将 derbyclient.jar
添加到您的 project classpath .
- 扩展您的项目。
- 右键单击库。
- 选择添加 Jar/文件夹。
- 选择“derbyclient.jar”
关于Java建立连接数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24289076/