我正在尝试使用Struts2建立jdbc连接。但我收到一个例外: java.sql.SQLException:没有为jdbc找到合适的驱动程序:postgresql:// localhost:5432 / postgres
这是我的代码:
package org.apache.struts.helloworld.action;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.DriverManager; import java.sql.ResultSet;
公共类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>
P.S。我使用Intellij Idea 14.1.4
有人知道原因吗?
答案 0 :(得分:1)
PostgreSQL的JDBC驱动程序是org.postgresql.Driver
所以
Class.forName("org.postgresql:postgresql:9.4.1207.jre7");
应该是
Class.forName("org.postgresql.Driver");
虽然这是一个JDBC 4驱动程序,你应该能够完全删除这个语句......