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

编程入门 行业动态 更新时间:2024-10-20 16:05:10
本文介绍了未找到类加载JDBC org.postgresql.Driver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个Web项目,我最近安装了postgres 9.1.1

postgresql服务器启动并运行。我可以像往常一样通过psql进行连接,并且所有内容都已加载并从我从8.5生成的db转储中正确保存。

所以我还下载了用于9.1 postgres的JDBC4驱动程序版本: http:// jdbc。 postgresql/download/postgresql-jdbc-9.1-901.src.tar.gz

我使用项目属性将其添加到java构建路径通过eclipse。

这是我用来提供与其他类的数据库连接的代码(即它是一个单例,只有当现有关闭或者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); //行触发类未找到异常 }否则if(connection.isClosed()){ connection = null; connect(); } } catch(SQLException e){ e.printStackTrace(System.err); } catch(例外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(); 返回连接; } } catch(SQLException e){ Logger.getLogger(DBConnection.class.getName())。log(Level.SEVERE, null,e); 返回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); 语句stmt = null; Logger.getLogger(Home.class.getName())。log(Level.SEVERE,验证用户凭据:用户名:+ id +密码:+ pswd); String sql =选择*来自fuser,其中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))返回新用户(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; } } 返回null; }

解决方案

我正在开发网络项目,我最近安装了postgres 9.1.1

...

我使用项目属性通过eclipse将其添加到java构建路径。

这是错误的方式。必须直接在Web项目的 / WEB-INF / lib 文件夹中删除JAR,而不必在项目属性中摆弄 Build Path 。该文件夹是webapp运行时类路径的标准部分。

无关具体问题:你你的 DBConnection 类中存在一个主要的设计缺陷。您已将 Connection 声明为 static ,这实际上使您的连接不是线程安全。使用连接池,永远不要分配连接(也不要语句,也不要 ResultSet )作为类/实例变量。它们应该在您执行查询的同一块 try-finally 块中创建和关闭。此外,你还有一个SQL注入漏洞。使用 PreparedStatement 而不是在SQL字符串中连接用户控制的变量。

另请参阅:
  • JDBC MySql连接池实践以避免耗尽连接池
  • 从连接池获取数据库连接
  • 我使用JDBC连接池吗?

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: jdbc.postgresql/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 to avoid exhausted connection pool
  • Get database connection from a connection pool
  • Am I Using JDBC Connection Pooling?

更多推荐

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

本文发布于:2023-10-25 08:42:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526466.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未找到   加载   JDBC   Driver   postgresql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!