未找到类加载JDBC org.postgresql.Driver
我正在开发一个Web项目,最近我安装了postgres 9.1.1
postgresql服务器已启动并正在运行。 我可以像往常一样通过psql连接,一切都被加载并从我从8.5制作的db的转储中正确保存。
所以我也在这里下载了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); } } } }
正如我在标题中写的那样,当我运行项目并且一个类要求连接到这个类时,我总是得到一个Class Not Found Exception,因为它显然无法加载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; }
我正在开发一个Web项目 ,最近我安装了postgres 9.1.1
…
我通过eclipse 使用项目属性将它添加到java构建路径中。
这是错误的方式。 必须直接将JAR放在Web项目的/WEB-INF/lib
文件夹中,而不必在项目属性中摆弄Build Path 。 该文件夹是webapp的运行时类路径的标准部分。
与具体问题无关 :您在DBConnection
类中存在一个主要的设计缺陷。 您已将Connection
声明为static
,这实际上使您的连接不是线程安全的 。 使用连接池,永远不要将Connection
(也不是Statement
或ResultSet
)分配为类/实例变量。 它们应该在与执行查询的try-finally
块完全相同try-finally
块中创建和关闭。 此外,你还有一个SQL注入漏洞。 使用PreparedStatement
而不是在SQL字符串中连接用户控制的变量。
也可以看看:
- JDBC MySql连接池实践,以避免耗尽连接池
- 从连接池获取数据库连接
- 我使用JDBC连接池吗?
我要做的第一件事就是解压jar并确认驱动程序确实在那里作为org.postgresql.Driver
。 我注意到jarfinder和相关网站没有包含org.postgresql.Driver
的Postgres 9.x jar。
在你的pom中添加这个依赖:
org.postgresql postgresql 9.4-1203-jdbc4
在主要课程中添加:
DriverManager.registerDriver(new org.postgresql.Driver());