Home > Mobile >  SQLException: No suitable driver found for database
SQLException: No suitable driver found for database

Time:10-16

I've tried to execute my java maven project but i have some trouble with connection with postgresql database; i use IntelliJ; i have included postgresql dependency in my pom and postgresql-42.2.24.jar in External libraries this is dependency in pom.xml

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

this is java code from DbHandler.java

public class DbHandler {
private static final String URLPOSTGRES = "jdbc:postgresql://localhost:5436/postgres";
private static final String URL = "jdbc:postgresql://localhost:5436/sbac";
private static final String USER = "postgres";
private static final String PSW = "Password";
private static Connection connection;

public DbHandler(){}
/**
* Method used to access or initialize the database in case never did before
* @author mattia
*/
public static void initDb(){
    boolean access = accessDb();
    if(!access){
        createDb();
        access = accessDb();
        if(access)
            createTables();
    }
}
/**
* Method used to access to the database 
* @return boolean true if access succesfull
* @author mattia
*/
private static boolean accessDb(){
    //accedo al DB  creato in precedenza
    try {
        connection = DriverManager.getConnection(URL, USER, PSW );
        if (connection != null) {System.out.println("Connected to the database!");}
        else {System.out.println("connection failed");}
        Class.forName("org.postgresql.Driver");
        System.err.println("Sbac : Connected to "  connection.getCatalog());
        return true;
    } catch (SQLException |ClassNotFoundException e) {
        System.err.println("Sbac : Error the database doesn't exist");
        return false;
    }
}
/**
* Method used to create the database
* @author mattia
*/
private static void createDb(){
     try {
         Connection con = DriverManager.getConnection(URLPOSTGRES, USER, PSW );
         if (con != null) {System.out.println("Connected to the database!");}
            else {System.out.println("connection failed");}
        Class.forName("org.postgresql.Driver");
        Statement sqlState = con.createStatement();
        String dbCreation ="create database sbac";
        sqlState.executeUpdate(dbCreation);
        System.err.println("Sbac : Created Db  and connected to it ");
    } catch (ClassNotFoundException | SQLException e) {
        System.err.println("Sbac : Failed to create Db\n" e);
    }
}
//...other methods
}

i created the executable jar file with mvn install and tried to execute the jar from cmd but i get some error

    C:\Users\User\Desktop\sbac-master\target>java -jar SmellAmongCommits-1.0-SNAPSHOT.jar
Sbac : Error the database doesn't exist
Sbac : Failed to create Db
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5436/postgres
Sbac : Error the database doesn't exist

CodePudding user response:

Please use stack trace for Exception, it is more detailed

USE StackTrace

  public static String getStackTrace(Exception e) {
    try {
        StringWriter outError = new StringWriter();
        e.printStackTrace(new PrintWriter(outError));
        String errorString = outError.toString();
        return errorString;
    } catch (Exception ec1) {

        // AMK . , So turkish . AQ
    }
    return "getStackTrace - failed ";
}

CodePudding user response:

You should check the deployed content. Maybe Deployment does not contain dependencies.

So another problem is that : Are you using PORT 5436 port postgres ?

Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(URLPOSTGRES, USER, PSW );

This is wrong java -jar SmellAmongCommits-1.0-SNAPSHOT.jar

You should add

java -cp .;postgresql.jar

AND MOR IMPORTANT .

  • Related