2013-04-09 107 views
4

当我在tomcat上测试我的web服务时,收到“找不到合适的驱动程序”错误。根据各种教程的说明,我在lib文件夹中有JDBC .jar。这里是我的代码:没有找到合适的驱动程序Postgres JDBC

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> 

任何帮助表示赞赏谢谢!

+0

你从哪里得到postgresql JDBC驱动程序?你可以添加链接到那个? – CoolBeans 2013-04-09 21:56:49

+0

[Tomcat 5.5,Postgres和JDBC臭名昭着的“没有合适的驱动程序”例外](http://stackoverflow.com/questions/1911253/the-infamous-no-suitable-driver-exception-for-tomcat -5-5-postgres-and-jdbc) – CoolBeans 2013-04-09 21:58:08

+1

你在哪个lib目录下放?你为什么要使用DriverManager,因为你想从Tomcat数据源获取连接? – 2013-04-09 21:58:47

回答

6

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

更新:

查看本页面给它一​​个位操作系统的研究,你应该罚款 : ) http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

比而不是使用DriverManager的,你应该做一个查询(你没有的话),比得到的数据源的连接(你可以从你的代码中删除PWD,用户,和其他没有用过的东西):

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

谢谢!当我应用更改时,我将更新结果 – cstokes2 2013-04-10 04:17:29

+0

嗨,我对我的答案做了更新,以便您可以更好地理解如何做到这一点。 – 2013-04-10 17:00:52

+0

谢谢你这个伎俩! – cstokes2 2013-04-10 20:22:18

相关问题