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

我正在开发一个Web项目,最近安装了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构建路径中。

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

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;

}

回答:

我正在开发一个 ,最近安装了Postgres 9.1.1。

我 通过eclipse 将其添加到Java构建路径中。

那是错误的方式。必须/WEB-INF/lib将该JAR直接拖放到该Web项目的文件夹中,而不必摆弄项目属性中的“ 构建路径

”。该文件夹是webapp运行时类路径的标准部分。


具体问题

:您的DBConnection班级存在重大设计缺陷。您已声明Connectionstatic,这实际上使您的连接

。使用连接池,切勿将Connection(nor StatementResultSet)分配为类/实例变量。它们应该在与try-

finally执行查询相同的块中创建和关闭。此外,您还有一个SQL注入孔。使用PreparedStatement而不是连接SQL字符串中的用户控制变量。

以上是 未找到加载JDBC org.postgresql.Driver的类 的全部内容, 来源链接: utcz.com/qa/430407.html

回到顶部