java.sql.SQLException:没有为jdbc:derby找到合适的驱动程序: [英] java.sql.SQLException: No suitable driver found for jdbc:derby:
问题描述
我是jdbc的初学者...我在运行此代码时遇到问题:
I'm a beginner with jdbc ... I have a problem running this code :
此代码使用appache derby并为了使其工作我首先启动了德比服务器..
This code uses appache derby and in order to make it work I first started the derby server..
java -jar "C:\Program Files\Sun\JavaDB\lib\derbyrun.jar" server start
然后启动程序
java -classpath derbyclient.jar -jar TestDB.jar
我设置了类路径
C:\Program Files\Sun\JavaDB\lib\dby.jar
I set the class path C:\Program Files\Sun\JavaDB\lib\derby.jar
和我总是得到那个异常
java.sql.SQLException:没有为jdbc找到合适的驱动程序:derby:// localhost:1527 /
BOOKDB;在TESTDB在java.sql.DriverManager.getConnection创建=在java.sql.DriverManager.getConnection真
(DriverManager.java:602)
(DriverManager.java:185)
。 getConnection(TestDB.java:63)
在TestDB.runTest(TestDB.java:20)
在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);
}
}
推荐答案
使用 -jar
和 -classpath
java 命令>参数,忽略 -classpath
参数。请参阅 Java启动程序的文档。
When you invoke the java
command with the -jar
and -classpath
parameters, the -classpath
parameter is ignored. See the documentation for the Java launcher.
您可以使用:
Unix / Linux:
Unix/Linux:
java -classpath derbyclient.jar:TestDB.jar TestDB
Windows:
java -classpath derbyclient.jar;TestDB.jar TestDB
或制作一个将derbyclient.jar添加到类路径的清单。
or make a manifest which adds derbyclient.jar to the classpath.
这篇关于java.sql.SQLException:没有为jdbc:derby找到合适的驱动程序:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!