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