I am connecting to a postgreSQL database with java.
The user inputs a username and a password in the command line and then I test the connection. I have put a try-catch block around it, but it still prints an error message when the connection fails. I don't want the user to see this message, I just want to handle the error myself, as can usually be achieved by a try-catch construct. However, in this particular case the message gets printed anyway. How can I suppress the message here?
Relevant code:
import java.sql.*;
public class ChessDatabase {
private Connection chessDB;
private String username;
private String password;
ChessDatabase(String username, String password) {
this.username = username;
this.password = password;
}
public boolean isConnected() {
try {
chessDB = DriverManager.getConnection("jdbc:postgresql://[...]", username, password);
} catch (Exception e) {
return false;
}
return true;
}
}
Error message:
Jun 25, 2018 3:38:24 PM org.postgresql.core.v3.ConnectionFactoryImpl log
WARNING: SQLException occurred while connecting to [...]
org.postgresql.util.PSQLException: FATAL: PAM authentication failed for user "[...]"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:473)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:205)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:452)
at org.postgresql.Driver.connect(Driver.java:254)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at ChessDatabase.isConnected(ChessDatabase.java:35)
[...]
Jun 25, 2018 3:38:24 PM org.postgresql.Driver connect
SEVERE: Connection error:
org.postgresql.util.PSQLException: FATAL: PAM authentication failed for user "[...]"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:473)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:205)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:452)
at org.postgresql.Driver.connect(Driver.java:254)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at ChessDatabase.isConnected(ChessDatabase.java:35)
[...]
PS: Just for clarification, the connection works and no error message is printed if username and password are correct. So the issue is not getting rid of the error, just of the message.