我有一个数据库项目,我试图从netbeabs连接到数据库,我创建了一个连接类。
公共类全局{
public static Connection createConnection() {
Connection conn = null;
System.out.println("Checking if Driver is registered with DriverManager.");
try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e){
System.out.println("Couldn't find the driver!");
System.out.println("exit.");
System.exit(1);
}
System.out.println("make a connection.");
try{
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/ydb_lib", "postgres", "postgres");
System.out.println("Success.");
} catch(SQLException e){
System.out.println("Couldn't connect: exit.");
System.exit(1);
}
return conn;
}}
我用这个方法调用这个类;
Connection c = Global.createConnection();运行程序后,输出为;
Checking if Driver is registered with DriverManager.
Couldn't find the driver!
exit.我看不出我的用户名,密码,驱动程序名,数据库名是正确的,所以你认为是什么问题??
发布于 2014-01-29 07:28:04
问题可能是由于项目在运行时找不到posrtgesql JDBC驱动程序。
因此,在类库的路径中添加jar。在这个网址上获得一个PostgreSQL JDBC驱动程序:http://jdbc.postgresql.org/download.html
发布于 2014-01-29 07:27:02
数据库驱动程序jar不在类路径上。
也请打印出您的异常,而不是仅仅忽略它们和打印您自己的错误消息。这能帮你省去很多头痛。
发布于 2014-01-29 07:27:13
org.postgresql.Driver类不在您的类路径中。
https://stackoverflow.com/questions/21424607
复制相似问题