【问题标题】:connect postgresql with java [duplicate]用java连接postgresql [重复]
【发布时间】:2013-05-05 12:37:21
【问题描述】:
package vas;

import java.sql.*;
public class ConnTO {

    public Connection conn;
    private String db;


    public ConnTO(String db) throws Exception{

        this.db=db;
        //Trying to get the driver
        try {
            Class.forName("org.postgresql.Driver");



        }
        catch (java.lang.ClassNotFoundException e) {
            java.lang.System.err.print("ClassNotFoundException: Postgres Server JDBC");
            java.lang.System.err.println(e.getMessage());
            throw new Exception("No JDBC Driver found in Server");
        }

        //Trying to connectpostgresql:/
        try {
            conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"+db,"postgres","211271");
            //conn.setCatalog(db);
            System.out.println("Connection with: "+db+"!!");
        }
        catch (SQLException E) {

            java.lang.System.out.println("SQLException: " + E.getMessage());
            java.lang.System.out.println("SQLState: " + E.getSQLState());
            java.lang.System.out.println("VendorError: " + E.getErrorCode());

        }
    }


    //Close Conn
    public void close() throws SQLException{

        try {
            conn.close();
            System.out.println("Connection close ");
        } catch (SQLException E) {


            java.lang.System.out.println("SQLException: " + E.getMessage());
            java.lang.System.out.println("SQLState: " + E.getSQLState());
            java.lang.System.out.println("VendorError: " + E.getErrorCode());
            throw E;
        }

    }



}

您好,我用 postgresql 创建了一个数据库,我想用 java 连接它,但是当我运行它时,会出现诸如

之类的错误
ClassNotFoundException: Postgres Server JDBCorg.postgresql.Driver
java.lang.Exception: No JDBC Driver found in Server
    at vas.ConnTO.<init>(ConnTO.java:21)
    at vas.main.login(main.java:17)
    at vas.main.main(main.java:51)

我还有一个主类,我将其称为 connto 类,并给出我的基类的名称

【问题讨论】:

标签: java postgresql


【解决方案1】:

在您的类路径中找不到类 org.postgresql.Driver。此类应位于类路径中的 jar 文件中。

【讨论】:

  • 感谢您的建议!
猜你喜欢
  • 2021-09-14
  • 2016-12-09
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多