Postgresql / Eclipse ERROR找不到合适的jbdc驱动程序

时间:2017-10-24 10:41:41

标签: java eclipse postgresql jdbc classpath

这是我为数据库连接编写的代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class StockReduzido {
 // Select "jdbc:postgresql://localhost/tbd" if using a local database

 private static final String LIGACAO ="jdbc:postgresql://localhost:5432/postgres"; //URL
 private static final String UTILIZADOR = "postgres"; //USER
 private static final String SENHA = "meio5"; //PASS
 private static final String INTERROGACAO = "SELECT ref, desig,stock " + "FROM produto WHERE (stock < 5) ORDER BY stock ASC";
 public static void main(String args[])
 throws SQLException {
 Connection objLigacao = null;
 Statement objComando = null;
 ResultSet objResultado = null;
 int intRef = 0;
 String strDesig = "";
 int intStock = 0;
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
 objLigacao = DriverManager.getConnection(LIGACAO,
UTILIZADOR, SENHA);
 objComando = objLigacao.createStatement();
 objResultado = objComando.executeQuery(INTERROGACAO);
 while (objResultado.next()) {
 intRef = objResultado.getInt("ref");
 strDesig = objResultado.getString("desig");
 intStock = objResultado.getInt("stock");
 System.out.println("Ref=" + intRef + "; " +
 "Desig=" + strDesig + "; " +
 "Stock=" + intStock + ";");
 }
 objResultado.close();
 objComando.close();
 objLigacao.close();
 }
}

然后我收到此错误:

java.lang.ClassNotFoundException: org.postgresql.Driver
    at java.lang.Class.forNameImpl(Native Method)
    at java.lang.Class.forName(Class.java:273)
    at StockReduzido.main(StockReduzido.java:24)
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgres
    at java.sql.DriverManager.getConnection(DriverManager.java:700)
    at java.sql.DriverManager.getConnection(DriverManager.java:258)
    at StockReduzido.main(StockReduzido.java:28)

我的postgres服务器是localhost:5432。但是我的数据库名称是“postgres”。我是否需要在URL连接中进行一些更改?

0 个答案:

没有答案