gpt4 book ai didi

java.sql.SQLException : No suitable driver found for jdbc:derby:

转载 作者:行者123 更新时间:2023-12-02 15:44:59 25 4
gpt4 key购买 nike

我是 jdbc 的初学者...我运行此代码时遇到问题:

此代码使用 appache derby,为了使其工作,我首先启动了 derby 服务器..

      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\derby.jar

我总是遇到这种异常

java.sql.SQLException:找不到适用于 jdbc:derby://localhost:1527/的驱动程序BOOKDB;创建=真 在 java.sql.DriverManager.getConnection(DriverManager.java:602) 在 java.sql.DriverManager.getConnection(DriverManager.java:185) 在 TestDB.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 参数被忽略。请参阅documentation for the Java launcher .

您可以使用:

Unix/Linux:

java -classpath derbyclient.jar:TestDB.jar TestDB

Windows:

java -classpath derbyclient.jar;TestDB.jar TestDB

或者制作一个 list ,将 derbyclient.jar 添加到类路径中。

关于java.sql.SQLException : No suitable driver found for jdbc:derby:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1375954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com