tomcat 没有找到合适的驱动程序Postgres JDBC

rekjcdws  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(85)

当我在tomcat上测试我的web服务时,我收到一个“没有找到合适的驱动程序”错误。我在lib文件夹中有JDBC .jar,就像各种教程说的那样。下面是我的代码:

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();
}

字符串
这里还有我按照Tomcat网站上的说明创建的web.xml和context.xml文件:

<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>


任何帮助是赞赏感谢!

ca1c2owp

ca1c2owp1#

正确的驱动程序名称是org.postgresql.Driver,而不是org.postgres.Driver
更新:
检查这个页面,给予它一点研究,你应该没问题:)http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
然后,而不是使用DriverManager,你应该做一个查找(你已经做了),并从数据源获得一个连接(你可以从你的代码中删除pwd,user和其他未使用的东西):

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

字符串

相关问题