我正在運行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