未找到类加载JDBC org.postgresql.Driver

时间:2011-10-26 15:11:22

标签: java postgresql jdbc

我正在开展一个网络项目,最近我安装了postgres 9.1.1

postgresql服务器已启动并正在运行。我可以像往常一样通过psql进行连接,并且所有内容都已加载并从我从8.5制作的db的转储中正确保存。

所以我也在这里下载了9.1 postgres版本的JDBC4驱动程序: http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-901.src.tar.gz

我通过eclipse使用项目属性将它添加到java构建路径中。

这是我用来提供与其他类的数据库连接的代码(即它是一个单例,只有当现有的关闭或为空时才获得新连接,一次只能从一个对象获得)

public abstract class DBConnection {
private static Connection connection = null;

public static void connect() {
    try {
        if (connection == null) {
            String host = "127.0.0.1";
            String database = "xxxxx";
            String username = "xxxxx";
            String password = "xxxxx";
            String url = "jdbc:postgresql://" + host + "/" + database;
            String driverJDBC = "org.postgresql.Driver";
            Class.forName(driverJDBC);
            connection = DriverManager.getConnection(url, username,
                    password); //line firing the class not found exception

        } else if (connection.isClosed()) {
            connection = null;
            connect();
        }
    } catch (SQLException e) {
        e.printStackTrace(System.err);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

public static void disconnect() {
    if (connection != null) {
        try {
            connection.close();
        } catch (SQLException e) {
            Logger.getLogger(DBConnection.class.getName()).log(
                    Level.SEVERE, null, e);
        }
        }
    }

    public static Connection getConnection() {

        try {
            if (connection != null && !connection.isClosed()) {
                return connection;
            } else {
                connect();
                return connection;
            }
        } catch (SQLException e) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE,
                    null, e);
            return null;
        }
    }

    @Override
    public void finalize() {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                Logger.getLogger(DBConnection.class.getName()).log(
                        Level.SEVERE, null, e);
            }
        }
    }

}

正如我在标题中写的那样,当我运行项目并且一个类要求连接到这个类时,我总是得到一个Class Not Found Exception,因为它显然无法加载org.postgresql.Driver.class驱动程序位于项目〜/ lib / org.postgresql-9.1-901.jdbc4.jar的子文件夹中,正如我所说,通过eclipse项目属性添加到构建路径中。

我还提供了一个示例查询,让我们看看我的类的通常行为来访问DBConnection:

public static final User validateUserCredentials(String id, String pswd) {
    Connection connection = DBConnection.getConnection();
    Logger.getLogger(Credentials.class.getName()).log(Level.SEVERE, (connection!=null)?"connection not null":"connection null");
    Statement stmt = null;
    Logger.getLogger(Home.class.getName()).log(Level.SEVERE, "validating credentials for user: username : " + id + " password : " + pswd);
    String sql = "Select * from fuser where id = '" + id + "'";
    ResultSet resultset = null;
    try {
        stmt = connection.createStatement();
        resultset = stmt.executeQuery(sql);
        Logger.getLogger(Credentials.class.getName())
                .log(Level.SEVERE, sql);
        resultset.next();
        String password = resultset.getString("pswd");
        if (pswd.equals(password))
            return new User(id, pswd);
    } catch (SQLException ex) {

        Logger.getLogger(Credentials.class.getName()).log(Level.SEVERE,
                null, ex);
    } finally {
        if (stmt != null)
            stmt = null;

        if (resultset != null)
            resultset = null;
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {

            }
            connection = null;
        }
    }
    return null;
}

3 个答案:

答案 0 :(得分:24)

  

我正在开发网络项目,我最近安装了postgres 9.1.1

     

...

     

我通过eclipse使用项目属性将其添加到java构建路径

这是错误的方式。必须将JAR直接放在Web项目的/WEB-INF/lib文件夹中,而不是在项目属性中摆弄 Build Path 。该文件夹是webapp运行时类路径的标准部分。


对具体问题

无关:您的DBConnection课程中存在重大设计缺陷。您已将Connection声明为static,这实际上使您的连接不是线程安全。使用连接池,永远不要将Connection(也不是StatementResultSet)指定为类/实例变量。应该在与执行查询的块try-finally块相同的块中创建和关闭它们。此外,你还有一个SQL注入漏洞。使用PreparedStatement而不是在SQL字符串中连接用户控制的变量。

另见:

答案 1 :(得分:3)

我要做的第一件事是解压缩jar并确认驱动程序确实在org.postgresql.Driver。我注意到jarfinder和相关网站没有包含org.postgresql.Driver的Postgres 9.x jar。

答案 2 :(得分:3)

在你的pom中添加这个依赖:

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1203-jdbc4</version>
    </dependency>