Java 使用JDBC连接到Derby
我正在尝试使用JDBC连接我的locahost上的Derby数据库 我已经使用命令启动了数据库:Java 使用JDBC连接到Derby,java,jdbc,derby,Java,Jdbc,Derby,我正在尝试使用JDBC连接我的locahost上的Derby数据库 我已经使用命令启动了数据库:java-jarlib;derbyrun.jar服务器启动,在端口1527上成功启动 在另一个命令终端上,我使用命令:java-classpath。;自由基;derbyclient.jar testsqldatabase.testsqldatabase但我得到以下错误: java.sql.SQLException: No suitable driver found for jdbc:postgresq
java-jarlib;derbyrun.jar服务器启动
,在端口1527上成功启动
在另一个命令终端上,我使用命令:java-classpath。;自由基;derbyclient.jar testsqldatabase.testsqldatabase
但我得到以下错误:
java.sql.SQLException: No suitable driver found for jdbc:postgresql:COREJAVA
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at testsqldatabase.TestSQLDatabase.getConnection(TestSQLDatabase.jav
)
at testsqldatabase.TestSQLDatabase.runTest(TestSQLDatabase.java:39)
at testsqldatabase.TestSQLDatabase.main(TestSQLDatabase.java:26)
My datatbase.properties文件包含以下行:
jdbc.drivers=org.postgresql.Driver
jdbc.url=jdbc:postgresql:COREJAVA
jdbc.username=dbuser
jdbc.password=secret
java程序如下所示:
public class TestSQLDatabase
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
try
{
runTest();
}
catch(SQLException ex)
{
for(Throwable t: ex)
t.printStackTrace();
}
}
/*Runs a test by creating a table, adding a value,
showing the table contents, removing the table*/
public static void runTest() throws SQLException, IOException
{
try(Connection conn = getConnection())
{
Statement stat = conn.createStatement();
stat.executeUpdate("CTEATE TABLE Greetings (Message CHAR(20))");
stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");
try(ResultSet result = stat.executeQuery("SELECT * FROM Greetings"))
{
if(result.next())
System.out.println(result.getString(1));
}
stat.executeUpdate("DROP TABLE Greetings");
}
}
/*
* Gets a connection from the properties specified in the
* file database.properties. @return the database connection
*/
public static Connection getConnection() throws SQLException, IOException
{
Properties props = new Properties();
try(InputStream in = Files.newInputStream(Paths.get("database.properties")))
{
props.load(in);
}
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);
}
}
有人能找出为什么我从第二个命令终端得到这个错误吗
谢谢,非常感谢Derby是一个数据库。PostgreSQL是一个不同的数据库。您正在运行一个Derby数据库,需要相应的Derby JDBC驱动程序与之通信,而不是PostgreSQL驱动程序。您希望使用PostgreSQL驱动程序(在属性文件中)连接到Derby;数据库的URL也写得不好;它应该是:
jdbc:${dataBaseVendor}:${server}:${port}/${databaseName}
还要确保类路径上有Derby JDBC驱动程序jar。是否要连接PostgreSQL数据库?好的,我知道如何使用两个不同的数据库。不,我想连接到Derby数据库。这是url的正确路径:jdbc.url=jdbc:derby:COREJAVA吗?我正在为Derby使用JDK 7包。谢谢你检查一下你的司机类型谢谢你的回复。好的,我知道我是如何使用两个不同的数据库的。这是url的正确路径:jdbc.url=jdbc:derby:COREJAVA吗?我正在为Derby使用JDK 7包。可能是谢斯奎特。我不太喜欢德比,所以我建议试试。不过看起来好多了!谢谢你的回复。好的,我知道我是如何使用两个不同的数据库的。Iv'e试图使用URL:jdbc.URL=jdbc:derby:COREJAVA,但是,我仍然得到了错误。我对JDBC非常陌生。我正在为Derby使用JDK 7包。Thanks@DouglasGrealisurl应该是这样的:
jdbc:derby:localhost:1527/COREJAVA
噢,我明白了,是的,我把那一行改成了正确的url。最后是我的客户端执行正确的命令:java-classpath。;自由基;derbyclient.jar testsqldatabase.testsqldatabase?谢谢,实际上我找到了正确的命令,现在它工作正常了。谢谢你的帮助