Ругается на то, что база данных не существует. Действительно, dbConnection
почему-то так и остаётся 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();
}
}
}
}
:postgresql::port/db
вот тут ошибка. укажите корректные данные. postgresql - это адрес базы, где она лежит, port - порт(число), db - имя схемы(базы)