我正在尝试使用 Struts2 建立一个 jdbc 连接。但我收到一个异常(exception): java.sql.SQLException:找不到适合 jdbc 的驱动程序:postgresql://localhost:5432/postgres
这是我的代码:
package org.apache.struts.helloworld.action;
导入 com.opensymphony.xwork2.ActionSupport;
导入java.sql.DriverManager; 导入 java.sql.ResultSet;
公共(public)类 HelloWorldAction 扩展 ActionSupport {
private static final long serialVersionUID = 1L;
/**
* The model class that stores the message
* to display in the view.
*/
//private MessageStore messageStore;
private String name;
/*
* Creates the MessageStore model object and
* returns success. The MessageStore model
* object will be available to the view.
* (non-Javadoc)
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
public String execute() {
String ret = ERROR;
java.sql.Connection connection = null;
try {
Class.forName("org.postgresql:postgresql:9.4.1207.jre7");
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "*******");
java.sql.Statement st = connection.createStatement();
ResultSet rt = st.executeQuery("SELECT * FROM provider where id=1");
while (rt.next()) {
// System.out.println(rt.getString(2));
name = rt.getString(2);
ret = SUCCESS;
}
rt.close();
st.close();
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
} catch (Exception e) {
System.out.println(e.getStackTrace());
return ERROR;
}
return ret;
}
我也有 maven 依赖
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1207.jre7</version>
</dependency>
附言我使用 Intellij Idea 14.1.4
有人知道原因吗?
最佳答案
PostgreSQL 的 JDBC 驱动是org.postgresql.Driver
所以
Class.forName("org.postgresql:postgresql:9.4.1207.jre7");
应该是
Class.forName("org.postgresql.Driver");
虽然这是一个 JDBC 4 驱动程序,但您应该能够完全删除此语句...
关于java - struts2 postgresql 找不到适合jdbc的驱动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102971/