0

I'm doing a program where I need to manipulate a PostgreSQL database. I downloaded and put the jdbc driver .jar in a lib folder and configured Intellij.

When I use Postgres JDBC driver from the Main.java, everything works fine. Here's an example that prints everything from example table :

public class Main {
    public static void main(String[] args) throws java.io.IOException {
        Class.forName("org.postgresql.Driver");
        Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Benerator","postgres","newPassword");
        PreparedStatement stmt = con.prepareStatement("select * from public.example");
        ResultSet res = stmt.executeQuery();
        while(res.next()){
            System.out.println(res.getString(1)+ " " + res.getString(2));}
}
}

However, when I write a function that does exactly the same thing in another class than Main, I receive a java.lang.ClassNotFoundException on the Class.forName line. And of course, every following line has exceptions.

Here is the DBManager.java class :

public class DBManager {

    public DBManager(){
    }

    public void printExample(){
        Class.forName("org.postgresql.Driver");
        Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Benerator","postgres","newPassword");
        PreparedStatement stmt = con.prepareStatement("select * from public.example");
        ResultSet res = stmt.executeQuery();
        while(res.next()){
            System.out.println(res.getString(1)+ " " + res.getString(2));}
    }
}

I think it's a newbie question but I had troubles finding the cause of that, any clues?

EDIT : when I surround my code with Try/Catch, everything works just fine

7
  • That sounds unlikely, your problem is probably somewhere different. By the way, you need to register the driver only once. Commented Aug 21, 2018 at 15:18
  • 1
    @LaurenzAlbe In a normal Java application you don't need to load the driver at all: it will be automatically loaded by DriverManager. Commented Aug 21, 2018 at 15:30
  • The error suggests that you did not add the postgresql library to the class path. Commented Aug 21, 2018 at 15:31
  • That is obviously not a duplicate. Commented Aug 21, 2018 at 15:38
  • @LaurenzAlbe Yes it was. I linked the generic duplicate for ClassNotFoundException and one specific to PostgreSQL that demonstrates a solution. Commented Aug 21, 2018 at 15:40

1 Answer 1

0
  1. In Intellij - goto File - Project Structure - Libraries. Make sure you can see the library jdbc if you're running through Intellij
  2. If you're running DBManager through a direct Java command then make sure to pass the -cp parameter like java -cp "somepath/lib/*" mypackage.MainClass
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I can actually see it because I added the jar to the libraries of the project in Intellij. And I'm not running the file from cl. I would instance a DBManager object in the Main class and then call the printExample() method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.