Swears that the database does not exist. Indeed, dbConnection for some reason remains null .

 public class CreatingConnection { private static Connection getDBConnection() { Connection dbConnection = null; try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); } try { dbConnection = DriverManager.getConnection("jdbc:postgresql::port/db","postgres", "user"); return dbConnection; } catch (SQLException e) { System.out.println(e.getMessage()); } return dbConnection; } static void createDbUserTable() throws SQLException { Connection dbConnection = null; Statement statement = null; String createTableSQL = "CREATE TABLE DBUSER(" + "USER_ID NUMBER(5) NOT NULL, " + "USERNAME VARCHAR(20) NOT NULL, " + "MESSAGE VARCHAR(20) NOT NULL, " + ")"; try { dbConnection = getDBConnection(); System.out.println(dbConnection); statement = dbConnection.createStatement(); statement.execute(createTableSQL); System.out.println("Table \"dbuser\" is created!"); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { if (statement != null) { statement.close(); } if (dbConnection != null) { dbConnection.close(); } } } } 
  • Did you check the connection settings? And the database exactly exists? Did you try to connect to it in some other way using the same data? - MANK
  • :postgresql::port/db here is an error. specify the correct data. postgresql is the address of the base where it lies, port is the port (number), db is the name of the schema (base) - Senior Pomidor

1 answer 1

:postgresql::port/db here is an error. specify the correct data.

postgresql is the address of the base where it is located,

port - port (number),

db - schema name (base)

example,

 connection = DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/testdb", "mkyong", "123456"); 
  • Thanks a lot!) Now there is a curse No suitable driver found for jdbc: postgesql: //127.0.0.1: 5432 / db - faster
  • Do you have a db scheme? Do you have all dependencies? - Senior Pomidor