java.sql.SQLException:找不到适用于 jdbc:derby 的驱动程序:
我是 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: No合适的驱动程序找到 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);
}
}
I'm a beginner with jdbc ... I have a problem running this code :
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
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
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)
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
-jar
和-classpath
参数调用java
命令时,-classpath
参数将被忽略。请参阅Java 启动器文档。您可以使用:
Unix/Linux:
Windows:
或制作一个清单,将 derbyclient.jar 添加到类路径中。
When you invoke the
java
command with the-jar
and-classpath
parameters, the-classpath
parameter is ignored. See the documentation for the Java launcher.You can either use:
Unix/Linux:
Windows:
or make a manifest which adds derbyclient.jar to the classpath.
当您使用
-jar
时,-classpath
将被忽略。来自java
命令工具文档:可以使用
-classpath
而不使用-jar
并显式指定包含 main 方法的类型,或者使您的 jar 文件清单 引用 derby jar 文件。When you use
-jar
,-classpath
is ignored. From thejava
command tool docs:Either use
-classpath
without-jar
and specify the type containing the main method explicitly, or make your jar file manifest reference the derby jar file.