resulta que estoy haciendo unas pruebas para iniciarme en el manejo de postgres a través de java, en el siguiente código:
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/testdb";
String user = "user12";
String password = "34klq*";
try (Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT VERSION()")) {
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
System.out.println("Error: "+ ex);
Logger lgr = Logger.getLogger(PostgresCommand.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
}
abr 06, 2018 8:28:04 AM org.postgresql.core.v3.ConnectionFactoryImpl log Error: org.postgresql.util.PSQLException: FATAL: no existe la base de datos «testdb» (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf) ADVERTENCIA: SQLException occurred while connecting to localhost:5432 org.postgresql.util.PSQLException: FATAL: no existe la base de datos «testdb» (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf) at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433) at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2566) at org.postgresql.core.v3.QueryExecutorImpl.(QueryExecutorImpl.java:131) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:210) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) at org.postgresql.jdbc.PgConnection.(PgConnection.java:195) at org.postgresql.Driver.makeConnection(Driver.java:452) at org.postgresql.Driver.connect(Driver.java:254) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at database.PostgresCommand.main(PostgresCommand.java:33)
abr 06, 2018 8:28:04 AM org.postgresql.Driver connect GRAVE: Connection error: org.postgresql.util.PSQLException: FATAL: no existe la base de datos «testdb» (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf) at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433) at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2566) at org.postgresql.core.v3.QueryExecutorImpl.(QueryExecutorImpl.java:131) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:210) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) at org.postgresql.jdbc.PgConnection.(PgConnection.java:195) at org.postgresql.Driver.makeConnection(Driver.java:452) at org.postgresql.Driver.connect(Driver.java:254) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at database.PostgresCommand.main(PostgresCommand.java:33)
abr 06, 2018 8:28:04 AM database.PostgresCommand main GRAVE: FATAL: no existe la base de datos «testdb» (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf) org.postgresql.util.PSQLException: FATAL: no existe la base de datos «testdb» (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf) at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433) at org.postgresql.core.v3.QueryExecutorImpl.readStartupMessages(QueryExecutorImpl.java:2566) at org.postgresql.core.v3.QueryExecutorImpl.(QueryExecutorImpl.java:131) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:210) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) at org.postgresql.jdbc.PgConnection.(PgConnection.java:195) at org.postgresql.Driver.makeConnection(Driver.java:452) at org.postgresql.Driver.connect(Driver.java:254) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at database.PostgresCommand.main(PostgresCommand.java:33)
Entiendo que esto surge debido a que la base de datos no existe pero si estoy capturando la excepción porque sigue arrojando el error? Como puedo solucionar esto?
Estoy tratando de verificar si la base de datos existe, en caso de que no exista crearla, esta parte la haré después por ahora me gustaría entender por qué no puedo capturar la excepción.