我有一个数据库项目,我试图从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:27:02
数据库驱动程序jar不在类路径上。
也请打印出您的异常,而不是仅仅忽略它们和打印您自己的错误消息。这能帮你省去很多头痛。
https://stackoverflow.com/questions/21424607
复制相似问题