未找到类加载 JDBC org.postgresql.Driver [英] Class not found loading JDBC org.postgresql.Driver
问题描述
我正在做一个网络项目,我最近安装了 postgres 9.1.1
I'm working on a web project and I recently installed postgres 9.1.1
postgresql 服务器已启动并正在运行.我可以像往常一样通过 psql 连接,一切都从我从 8.5 制作的数据库的转储中加载并正确保存.
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.
所以我也在这里下载了 9.1 postgres 版本的 JDBC4 驱动程序:http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-901.src.tar.gz
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
我通过 eclipse 使用项目属性将其添加到 java 构建路径中.
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);
}
}
}
}
正如我在运行项目时在标题中所写的那样,一个类要求连接到这个类,我总是得到一个 Class Not Found Exception,因为它显然无法加载 org.postgresql.Driver.class 驱动程序位于项目 ~/lib/org.postgresql-9.1-901.jdbc4.jar 的子文件夹中,正如我所说,通过 eclipse 项目属性添加到构建路径中.
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.
我还提供了一个示例查询,以便查看我的类访问 DBConnection 的通常行为:
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;
}
推荐答案
我正在从事一个网络项目,我最近安装了 postgres 9.1.1
I'm working on a web project and I recently installed postgres 9.1.1
...
我通过eclipse使用项目属性将它添加到java构建路径中.
I added it to the java build path using the project properties via eclipse.
那是错误的方式.该 JAR 必须直接放在 Web 项目的 /WEB-INF/lib
文件夹中,而无需在项目属性中修改 Build Path.该文件夹是 webapp 运行时类路径的标准部分.
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.
与具体问题无关:您的 DBConnection
类存在重大设计缺陷.您已将 Connection
声明为 static
,这实质上使您的连接不是线程安全的.使用连接池并且永远不要将 Connection
(也不是 Statement
或 ResultSet
)分配为类/实例变量.它们应该在与您执行查询的位置完全相同的 try-finally
块中创建和关闭.此外,您还有一个 SQL 注入漏洞.使用 PreparedStatement
而不是在 SQL 字符串中连接用户控制的变量.
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.
这篇关于未找到类加载 JDBC org.postgresql.Driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!