我正在运行Netbeans 8.0.2。我正在学习JDBC并希望将其连接到PostgreSQL数据库。我查找了所有可能的答案,但没有答案让它工作。如何解决'找不到合适的驱动程序'错误
我也选择库中的左侧菜单中的如PostgreSQL JDBC Driver -postgresql-9.2-1002.jdbc4.jar
错误显示为:
SQL exception occuredjava.sql.SQLException: No suitable driver found for Jdbc:postgresql://localhost:5432/postgres
下面的代码:
try {
Class.forName("org.postgresql.Driver");
}
catch(ClassNotFoundException e) {
System.out.println("Class not found "+ e);
}
try {
Connection con = DriverManager.getConnection
("Jdbc:postgresql://localhost:5432/postgres","postgres",
"gautam");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("SELECT * FROM role");
System.out.println("id name");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id+" "+name);
}
}
catch(SQLException e){
System.out.println("SQL exception occured" + e);
}
不知'JDBC:'是区分大小写的。 (我猜这不是问题,但试试吧......) – immibis
是的..它的工作! – Gautam