1

Ругается на то, что база данных не существует. Действительно, 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();
            }
        }
    }
}
2
  • А вы параметры подключения проверяли? И база данных точно существует? Вы к ней каким-то инным способом пробовали подключиться, используя те же данные?
    – МАН69К
    18 окт 2016 в 9:59
  • :postgresql::port/db вот тут ошибка. укажите корректные данные. postgresql - это адрес базы, где она лежит, port - порт(число), db - имя схемы(базы) 18 окт 2016 в 10:01

1 ответ 1

1

:postgresql::port/db вот тут ошибка. укажите корректные данные.

postgresql - это адрес базы, где она лежит,

port - порт(число),

db - имя схемы(базы)

пример,

connection = DriverManager.getConnection(
                "jdbc:postgresql://127.0.0.1:5432/testdb", "mkyong",
                "123456");
2
  • спасибо огромное!) теперь ругается No suitable driver found for jdbc:postgesql://127.0.0.1:5432/db
    – faster
    18 окт 2016 в 10:14
  • У тебя схема называется db? у тебя зависимости все имеются ? 18 окт 2016 в 11:22

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.