加载 JDBC org.postgresql.Driver 时找不到类
我正在开发一个网络项目,最近安装了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是错误的方式。该 JAR 必须直接放入 Web 项目的
/WEB-INF/lib
文件夹中,而无需修改项目属性中的构建路径。该文件夹是 webapp 运行时类路径的标准部分。与具体问题无关:您的
DBConnection
类中存在重大设计缺陷。您已将Connection
声明为static
,这实际上使您的连接不是线程安全的。使用连接池,切勿将Connection
(也不是Statement
或ResultSet
)分配为类/实例变量。它们应该在与执行查询的位置完全相同的try-finally
块中创建和关闭。此外,还有一个 SQL 注入漏洞。使用PreparedStatement
而不是在 SQL 字符串中连接用户控制的变量。另请参阅:
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 declaredConnection
asstatic
which essentially makes your connection not threadsafe. Use a connection pool and never assign theConnection
(norStatement
norResultSet
) as a class/instance variable. They should be created and closed in the very sametry-finally
block as where you're executing the query. Further you've there also a SQL injection hole. UsePreparedStatement
instead of concatenating user-controlled variables in the SQL string.See also:
在您的 pom 中添加此依赖项:
Add this dependency in your pom:
我要做的第一件事就是解压 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 containingorg.postgresql.Driver
.如果您遇到 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.