2014-01-29 217 views
0

我有一个数据库项目,我试图从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. 

我不能看到问题我的用户名,密码,驱动程序名称,数据库名称是正确的,所以你认为可能是什么问题?

+0

什么是您正在使用..确保DB驱动程序JAR文件是在你的lib文件夹中的数据基地。 – Babel

+0

是的,我认为这个问题的感谢,多数民众赞成@looser – amadeus

回答

0

您的数据库驱动程序jar不在类路径中。

还请打印出您的例外情况,而不是忽略它们并打印出自己的错误消息。这可以为您节省一笔负担。

0

org.postgresql.Driver类不在您的类路径中。