1
votes

I'm trying to set up a jdbc connection using Struts2. But I receive an exception: java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgres

Here is my code:

package org.apache.struts.helloworld.action;

import com.opensymphony.xwork2.ActionSupport;

import java.sql.DriverManager; import java.sql.ResultSet;

public class HelloWorldAction extends 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

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

P.S. I use Intellij Idea 14.1.4

Does anybody know the reason?

1

1 Answers

1
votes

The JDBC driver for PostgreSQL is org.postgresql.Driver so

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...