简体   繁体   English

没有找到Struts2 Postgresql

[英]Struts2 postgresql No suitable driver found for jdbc

I'm trying to set up a jdbc connection using Struts2. 我正在尝试使用Struts2建立一个jdbc连接。 But I receive an exception: java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgres 但是我收到一个异常:java.sql.SQLException:找不到适用于jdbc:postgresql:// localhost:5432 / postgres的驱动程序

Here is my code: 这是我的代码:

package org.apache.struts.helloworld.action;

import com.opensymphony.xwork2.ActionSupport; 导入com.opensymphony.xwork2.ActionSupport;

import java.sql.DriverManager; 导入java.sql.DriverManager; import java.sql.ResultSet; 导入java.sql.ResultSet;

public class HelloWorldAction extends ActionSupport { 公共类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;
}

I've got maven dependency as well 我也有Maven依赖

<dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1207.jre7</version>
    </dependency>

PS I use Intellij Idea 14.1.4 PS我使用Intellij Idea 14.1.4

Does anybody know the reason? 有人知道原因吗?

The JDBC driver for PostgreSQL is org.postgresql.Driver so PostgreSQL的JDBC驱动程序是org.postgresql.Driver因此

Class.forName("org.postgresql:postgresql:9.4.1207.jre7");

should be 应该

 Class.forName("org.postgresql.Driver");

although since this is a JDBC 4 driver you should be able to remove this statement completely... 尽管由于这是JDBC 4驱动程序,所以您应该可以完全删除此语句...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM