找不到合适的驱动程序Postgres JDBC [英] No Suitable Driver found Postgres JDBC

查看:121
本文介绍了找不到合适的驱动程序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 .org / tomcat-6.0-doc / jndi-datasource-examples-howto.html

Check this page give it a bit os study and you should be fine :) http://tomcat.apache.org/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆