找不到合适的驱动程序Postgres JDBC

编程入门 行业动态 更新时间:2024-10-28 02:31:44
本文介绍了找不到合适的驱动程序Postgres JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在tomcat上测试我的Web服务时,我收到找不到合适的驱动程序错误。如各种教程所述,我在lib文件夹中有JDBC .jar。这是我的代码:

I am receiving a "no suitable driver found" error when I test my web service on tomcat. I have the JDBC .jar in the lib folder as various tutorials says to do. Here is my code:

public class PostDBConnection { PreparedStatement st; ResultSet rs; Connection con; DataSource ds; InitialContext cxt; String url = "jdbc:postgresql://127.0.0.1:5432/UptonDB"; String user = "*****"; String password = "*******"; String query = ""; StringBuilder response = new StringBuilder(); @SuppressWarnings("unused") public String getInfo(){ int size = 0; try { cxt = new InitialContext(); ds = (DataSource) cxt.lookup("java:comp/env/jdbc/UptonDB"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ try { Class.forName("org.postgres.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } con = DriverManager.getConnection(url, user, password); st = con.prepareStatement("SELECT VERSION()"); rs = st.executeQuery(); while(rs.next()) { response.append(rs.getString(1)); } } catch(SQLException exc) { Logger lgr = Logger.getLogger(PostDBConnection.class.getName()); lgr.log(Level.SEVERE, exc.getMessage(), exc); } finally { try { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException ex) { Logger lgr = Logger.getLogger(PostDBConnection.class.getName()); lgr.log(Level.WARNING, ex.getMessage(), ex); } } return response.toString(); }

这也是我按照以下步骤创建的web.xml和context.xml文件Tomcat网站上的说明:

Also here are the web.xml and context.xml files I created by following instructions on the Tomcat website:

<resource-ref> <description>PostgreSQL Data Source </description> <res-ref-name>jdbc/UptonDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>

web.xml:

<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource name="jdbc/UptonDB" auth="Container" type="javax.sql.DataSource" removeAbandoned="true" removeAbandonedTimeout="30" maxActive="80" maxIdle="30" maxWait="10000" username="*****" password="*******" driverClassName="org.postgresql.Driver" url = "jdbc:postgresql://127.0.0.1:5432/UptonDB" useUnicode="true" characterEncoding="utf-8" characterSetResults="utf8"/> </Context>

感谢任何帮助!

推荐答案

正确的驱动程序名称是: org.postgresql.Driver 而不是org.postgres.Driver

The proper Driver name is: org.postgresql.Driver and not org.postgres.Driver

更新:

检查此页进行一些操作系统研究,您应该没事 :) http://tomcat.apache / tomcat-6.0-doc / jndi-datasource-examples-howto.html

Check this page give it a bit os study and you should be fine :) tomcat.apache/tomcat-6.0-doc/jndi-datasource-examples-howto.html

比起使用DriverManager,您应该做一个查找(并且已经从DataSource获得连接(您可以从代码中删除pwd,用户和其他未使用的内容):

Than instead of using DriverManager, you should just do a lookup(you did already) and than get a connection from the DataSource(You can remove pwd, user, and other unused stuff from your code):

Context initContext = new InitialContext(); Context envContext = (Context)initContext.lookup("java:/comp/env"); DataSource ds = (DataSource)envContext.lookup("jdbc/UptonDB"); Connection conn = ds.getConnection();

更多推荐

找不到合适的驱动程序Postgres JDBC

本文发布于:2023-10-18 23:39:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1505754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   驱动程序   合适   JDBC   Postgres

发布评论

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

>www.elefans.com

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