-1

I'm trying to connect to my postgresql database on my localhost with Java and JDBC. But getting error that is no suitable driver found for this. I successfully connected to the database in the IntelliJ IDEA through in the "Database" tab, but I cannot connect in the code.

This is my Main.java:

package org.example;

import java.sql.Connection;
import java.sql.DriverManager;

public class Main {

    public static final String url="jdbc:postgresql://localhost:1234/testDatabase";
    public static final String user="postgres";
    public static final String password="1234maev";

    public static void main(String[] args) {
        Connection connection = null;

        try {
            connection = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the PostgreSQL server successfully.");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

I've JDK version 1.8 and downloaded the .jar file with the postgresql JDBC driver version 42.6.0, included it in the libraries and in the modules (screenshots below). Screenshot from "Modules" tab Screenshot from "Libraries" tab

How can I fix it?

I expected that my java app will connect to the database. I tried to add driver again, but it didn't work. Connection through the "Databases" tab in IntelliJ IDEA with host, port, user and password worked fine.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Mikhail
  • 9
  • 3
  • 2
    Please [reconsider your use of JDBC on Android](https://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android). – CommonsWare May 29 '23 at 18:32
  • Are you actually developing for Android? Or is that a bad tag and you're on your local machine? A `main` like you have will not work on Android and certainly not to `localhost`. – stdunbar May 29 '23 at 19:26

1 Answers1

0

If your project is a maven project you should add the driver to your pom.xml like this:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.6.0</version>
</dependency>

Probably should be helpful if you post your project structure.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
jalfonso
  • 14
  • 1