java.sql.SQLException: 找不到适合 jdbc:derby 的驱动程序:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1375954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: No suitable driver found for jdbc:derby:
提问by Jon Skeet
I'm a beginner with jdbc ... I have a problem running this code :
我是 jdbc 的初学者......运行此代码时遇到问题:
This code uses appache derby and in order to make it work I first started the derby server..
这段代码使用了 appache derby,为了让它工作,我首先启动了 derby 服务器。
java -jar "C:\Program Files\Sun\JavaDB\lib\derbyrun.jar" server start
And then started the program
然后启动程序
java -classpath derbyclient.jar -jar TestDB.jar
I set the class path C:\Program Files\Sun\JavaDB\lib\derby.jar
我设置了类路径 C:\Program Files\Sun\JavaDB\lib\derby.jar
And I'm always getting that exception
我总是得到那个例外
java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/ BOOKDB;create=true at java.sql.DriverManager.getConnection(DriverManager.java:602) at java.sql.DriverManager.getConnection(DriverManager.java:185) at TestDB.getConnection(TestDB.java:63) at TestDB.runTest(TestDB.java:20) at TestDB.main(TestDB.java:11)
java.sql.SQLException: 没有找到适合 jdbc:derby://localhost:1527/ BOOKDB 的驱动程序;create=true at java.sql.DriverManager.getConnection(DriverManager.java:602) at java.sql.DriverManager.getConnection( DriverManager.java:185) at TestDB.getConnection(TestDB.java:63) at TestDB.runTest(TestDB.java:20) at TestDB.main(TestDB.java:11)
import java.sql.*;
import java.io.*;
import java.util.*;
class TestDB
{
public static void main(String args[])
{
try
{
runTest();
}
catch (SQLException ex)
{
for (Throwable t : ex)
t.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static void runTest() throws SQLException, IOException
{
Connection conn = getConnection();
try
{
Statement stat = conn.createStatement();
stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))");
stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");
ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
if (result.next())
System.out.println(result.getString(1));
result.close();
stat.executeUpdate("DROP TABLE Greetings");
}
finally
{
conn.close();
}
}
public static Connection getConnection() throws SQLException, IOException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null) System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url, username, password);
}
}
回答by Simon Nickerson
When you invoke the javacommand with the -jarand -classpathparameters, the -classpathparameter is ignored. See the documentation for the Java launcher.
当您java使用-jar和-classpath参数调用命令时,该-classpath参数将被忽略。请参阅Java 启动器的文档。
You can either use:
您可以使用:
Unix/Linux:
Unix/Linux:
java -classpath derbyclient.jar:TestDB.jar TestDB
Windows:
视窗:
java -classpath derbyclient.jar;TestDB.jar TestDB
or make a manifest which adds derbyclient.jar to the classpath.
或者制作一个清单,将 derbyclient.jar 添加到类路径。
回答by Jon Skeet
When you use -jar, -classpathis ignored. From the javacommand tool docs:
使用时-jar,将-classpath被忽略。从java命令工具文档:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
使用此选项时,JAR 文件是所有用户类的来源,其他用户类路径设置将被忽略。
Either use -classpathwithout -jarand specify the type containing the main method explicitly, or make your jar file manifestreference the derby jar file.
要么使用-classpathwithout-jar并明确指定包含 main 方法的类型,要么使您的jar 文件清单引用 derby jar 文件。

