当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Class not found loading JDBC org.postgresql.Driver

问题描述:

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;

}

网友答案:

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:

  • JDBC MySql Connection Pooling practices
  • Get database connection from a Connection Pool
  • Am I Using JDBC Connection Pooling?
网友答案:

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.

网友答案:

Add in main class:

DriverManager.registerDriver(new org.postgresql.Driver());
网友答案:

Add this dependency in your pom:

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1203-jdbc4</version>
    </dependency>
分享给朋友:
您可能感兴趣的文章:
随机阅读: