找不到适用于jdbc:postgresql:// localhost:51020 / university的驱动程序

时间:2019-10-06 22:29:11

标签: java postgresql jdbc

import java.sql.*;
public class a2 {
  public static void main(String[] args) {
    try{
      Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/univ_db", "postgre", "astronaut.pd");
      PreparedStatement pstmt = con.prepareStatement("select id from student");
      ResultSet Rs = pstmt.executeQuery();
      while(Rs.next()){
        System.out.println(Rs.getInt(5));
      }
    }catch(Exception ex){
      System.out.println(ex.getMessage());
    }}}

我正在尝试将服务器与数据库连接,但我不知道我在这方面做错了什么。我试图在线搜索它,但没有找到任何解决方案。

错误:

  

找不到适用于jdbc:postgresql:// localhost:5432 / univ_db的驱动程序

1 个答案:

答案 0 :(得分:0)

要解决此错误,您需要在使用using Class.forName()进行连接之前获取驱动程序类引用DriverManager.getConnection()

示例:

Class.forName(“org.postgresql.Driver”);

重要提示:请记住将postgree驱动程序导入到Java项目中。

相关问题