1

I've been working on a web project with Java EE and PostgreSQL and it runs perfectly. I tried moving it to another machine, but now it's not connecting to the database anymore.

I tried multiple solutions but nothing works so far:

  • adding postgresql.jar to java build path
  • moving it to the /lib file of tomcat server

Hers is my connection to database class:

public class DBConnexion {
    private static Connection con=null;


private DBConnexion(){
    try
    {
        try {
            Class.forName("org.postgresql.Driver");
            //getConnection(url:dataBase name, owner name , password) 
            con=(Connection)DriverManager.getConnection("jdbc:postgresql:septentrion", "postgres","123");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    catch(SQLException e1)
    {
        e1.printStackTrace();
    }
}

public static Connection getInstance()
{
    if(con==null)
        new DBConnexion();
    return con;
}



}
Mysterious Wolf
  • 373
  • 1
  • 5
  • 22
lina
  • 53
  • 2
  • 8

1 Answers1

2

Depending on your dependency manager, you will have to add postgresql as your dependency. For example if you are using Maven then

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

If you are not using anything(which I think is the case), then you will have to manually download the jar file and put it in your classpath.

EDIT: Since you do not use any dependency manager, if you want to compile com.example.Foo that depends on lib/postgresql-42.2.6.jar you might use the following incantation:

javac -classpath lib/postgresql-42.2.6.jar com/example/Foo.java

Ashvin Sharma
  • 563
  • 1
  • 5
  • 24
  • Yes , i'm not using anything, and i manually download the jar file and put it in the classpath but it still not working – lina Jul 24 '19 at 08:40
  • Yes,i do use eclipse – lina Jul 24 '19 at 08:45
  • @lina check the updated answer – Ashvin Sharma Jul 24 '19 at 08:46
  • i'm actually on windows , ind i run it from the **Run As** button :/ – lina Jul 24 '19 at 08:58
  • Doesnt matter, javac command runs on any machine as long as you have java installed. EDIT- To make it more simple to you [here](https://www.edureka.co/community/4028/how-to-import-a-jar-file-in-eclipse) is the link which will help you. Bear in mind your programming style is far below the accepted standards and will depend upon one IDE which is not the ideal case. I would strongly suggest learning more about java and dependency manager if you want to continue to write programs in java – Ashvin Sharma Jul 24 '19 at 09:10
  • Yes it is installed but doesn't run – lina Jul 24 '19 at 09:13
  • It is because you didnt add it to the PATH – Ashvin Sharma Jul 24 '19 at 09:14
  • I don't know what's wrong with this machine , nothing works , thank you for your help – lina Jul 24 '19 at 09:56
  • @line Given you tagged java-ee: you need to make sure the driver is correctly deployed to your application server. – Mark Rotteveel Jul 24 '19 at 11:47
  • @AshvinSharma i'm still a begineer , what do you mean by the **accepted standards** ?? – lina Jul 24 '19 at 11:52
  • There's no concrete meaning of "accepted standards". What I meant by the phrase is best practices which will make your program easy to write and maintain. – Ashvin Sharma Jul 24 '19 at 11:54
  • @AshvinSharma i see ^^ thank you – lina Jul 24 '19 at 11:59