PostgreSQL / Java driver issues

106 Views Asked by At

Newbie here. I've been following the PostgreSQL instructions and tried to connect my application to the database, but it seems like no suitable driver was found.. I located the driver file but I am not sure where to put the jar file. Any suggestions?

Additional details:

I've checked the password and the connection string, and the "New Connection Wizard" via Netbeans reports that the connection is fine.

When I've researched other tutorials, many of the sources seemed to be working with older version of Netbeans where the driver is added through the "library" tab in the project property window. I am working with Netbeans 12.0, which does not allow me to add jar files in the library tab (because the tab does not exist anymore). Does this have to do with Netbeans 12.0 forcing me to create java projects with Maven, Ant, or Gradle. ? If this helps, here is the pome file.

Thanks, here's the code:

    public class Main {
       private  String database_connection_string =
            "jdbc:postgresql://localhost:5432/postgres [postgres on public]";
       private  String database_user_name = "postgres";
       private  String database_user_password = "<mypasswordhere>";

    public Connection connect() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(
                    database_connection_string, 
                    database_user_name, 
                    database_user_password);
            System.out.println(
                    "You are successfully connected to the PostgreSQL database server.");
        } catch (SQLException e) {
            System.out.println(e.getMessage());    
        }
        return conn;
    }

    
    public static void main(String[] args) {
        Main conn = new Main();
        conn.connect();    
    }
}
1

There are 1 best solutions below

0
Cebrail Yilmaz On

If you prefer to go with maven then you can add the postgresql maven dependency, just below the <properties></properties> and above the </project> tags. Last part of your pom.xml file should look something like as follows;

    ...
    </properties>
    <dependencies>
        <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.14</version>
        </dependency>
    </dependencies>
</project>

Add this lines, rebuild the project and see if this helps. If this does'nt solve your problem please let me know, so I can delete my answer.(I post as an answer because I don't have enough reputation to comment.)