postgresql 找不到合适的驱动程序 Postgres JDBC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15913253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No Suitable Driver found Postgres JDBC
提问by cstokes2
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:
我在 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();
}
Also here are the web.xml and context.xml files I created by following instructions on the Tomcat website:
这里还有我按照 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:
<?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>
Any help is appreciated thanks!
任何帮助表示赞赏谢谢!
回答by groo
The proper Driver name is: org.postgresql.Driverand not org.postgres.Driver
正确的驱动程序名称是:org.postgresql.Driver而不是 org.postgres.Driver
Update:
更新:
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
检查这个页面给它一些操作系统研究,你应该没问题:) http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
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):
与其使用 DriverManager,您应该只进行查找(您已经这样做了),然后从数据源获取连接(您可以从代码中删除密码、用户和其他未使用的内容):
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/UptonDB");
Connection conn = ds.getConnection();