加载 JDBC org.postgresql.Driver 时找不到类

发布于 2024-12-12 02:09:33 字数 3993 浏览 0 评论 0原文

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

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

所以我还在这里下载了 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);
            }
        }
    }

}

正如我在标题中所写的那样运行项目并且类要求连接到此类我总是收到类未找到异常,因为它显然无法加载 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;
}

I'm working on a web project and I recently installed postgres 9.1.1

The postgresql server is up and running. I can connect via psql as usual and everything is loaded and properly saved from a dump of the db I made from 8.5.

So I also downloaded the JDBC4 driver for 9.1 postgres version here:
http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-901.src.tar.gz

I added it to the java build path using the project properties via eclipse.

This is the code I use to provide db connection to other classes (i.e. it's a singleton, I get a new connection only if the existing is either closed or null, from one object at a time only)

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);
            }
        }
    }

}

As I wrote in the title when I run the project and a class asks for a connection to this class I always get a Class Not Found Exception, Since it apparently can't load the org.postgresql.Driver.class The driver is located in a subfolder of the project ~/lib/org.postgresql-9.1-901.jdbc4.jar and as I said added to the build path via eclipse project properties.

I'm also providing a sample query to let see the usual behavior of my classes to access the 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

私野 2024-12-19 02:09:33

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

...

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

这是错误的方式。该 JAR 必须直接放入 Web 项目的 /WEB-INF/lib 文件夹中,而无需修改项目属性中的构建路径。该文件夹是 webapp 运行时类路径的标准部分。


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

另请参阅:

I'm working on a web project and I recently installed postgres 9.1.1

...

I added it to the java build path using the project properties via eclipse.

That's the wrong way. That JAR has to be dropped straight in /WEB-INF/lib folder of the web project without fiddling with the Build Path in the project's properties. That folder is standard part of webapp's runtime classpath.


Unrelated to the concrete problem: you've a major design flaw in your DBConnection class. You've declared Connection as static which essentially makes your connection not threadsafe. Use a connection pool and never assign the Connection (nor Statement nor ResultSet) as a class/instance variable. They should be created and closed in the very same try-finally block as where you're executing the query. Further you've there also a SQL injection hole. Use PreparedStatement instead of concatenating user-controlled variables in the SQL string.

See also:

简单 2024-12-19 02:09:33

在您的 pom 中添加此依赖项:

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

Add this dependency in your pom:

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1203-jdbc4</version>
    </dependency>
無心 2024-12-19 02:09:33

我要做的第一件事就是解压 jar 并确认驱动程序确实在其中,即 org.postgresql.Driver。我注意到,在查看 jarfinder 和相关站点时,没有包含 org.postgresql.Driver 的 Postgres 9.x jar。

The first thing I'd do is unpack the jar and confirm that the driver is really in there as org.postgresql.Driver. I notice when looking at jarfinder and related sites that there isn't a Postgres 9.x jar containing org.postgresql.Driver.

别理我 2024-12-19 02:09:33

如果您遇到 jpa.properties.hibernate.format_sql 无法工作的任何问题,只需确保您的 java jdk 正常工作,然后尝试运行该项目,IntelliJ 会要求为您修复它。

If you have any issues where the jpa.properties.hibernate.format_sql isn't working either just make sure that you have your java jdk working so try running the project and IntelliJ will ask to fix it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文