title: Class not found loading JDBC org.postgresql.Driver
body: <p>I'm working on a web project and I recently installed postgres 9.1.1</p>
  
  <p>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.</p>
  
  <p>So I also downloaded the JDBC4 driver for 9.1 postgres version here:
  <a href="http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-901.src.tar.gz" rel="noreferrer">http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-901.src.tar.gz</a></p>
  
  <p>I added it to the java build path using the project properties via eclipse.</p>
  
  <p>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)</p>
  
  <pre><code>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 &amp;&amp; !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);
              }
          }
      }
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>I'm also providing a sample query to let see the usual behavior of my classes to access the DBConnection:</p>
  
  <pre><code>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;
  }
  </code></pre>