8

I have the following code to connect to the database

String host = "jdbc:postgresql://localhost:5432/name";
    String username = "user";
    String password = "pass";
    Connection c = null;
    try {
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection(host, username, password);
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    System.out.println("Opened database successfully");
}

and I'm getting the following error:

org.postgresql.util.PSQLException: El intento de conexión falló.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:257)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:149)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:35)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:47)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:30)
at org.postgresql.Driver.makeConnection(Driver.java:414)
at org.postgresql.Driver.connect(Driver.java:282)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at database_console.DBConnect.main(DBConnect.java:22)
Caused by: java.io.IOException: Illegal UTF-8 sequence: byte 2 of 4 byte sequence is not 10xxxxxx: 110
at org.postgresql.core.UTF8Encoding.checkByte(UTF8Encoding.java:28)
at org.postgresql.core.UTF8Encoding.decode(UTF8Encoding.java:117)
at org.postgresql.core.PGStream.ReceiveString(PGStream.java:327)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:424)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:203)
... 11 more
org.postgresql.util.PSQLException: El intento de conexión falló.

"el intento de conexión falló" means "conection attempt failed".

Please help me with this I don't know what to do.

EDIT: I also checked the server encoding and it says it's UTF8

  • The user and password are really "user" and "pass" or do they contain any non-ascii characters? Also, what is the version of the server, and the JDBC driver? – RealSkeptic Nov 18 '15 at 19:06
  • The user and pass are a combination of 4 letters and numbers, nothing weird. The JDBC driver version is 9.4-1205 jdbc4 and the server version is 9.1.14 – Diego Rodriguez Nov 18 '15 at 19:18
  • Are you sure that Postgresql is running on port 5432 on your machine? Could there be something else running on that port? – RealSkeptic Nov 18 '15 at 19:38
  • 1
    I had this exception with empty password. – CHEM_Eugene Apr 14 '16 at 14:55

5 Answers5

1

I think the problem could be that java strings are UTF-16 and Postgresql needs UTF-8 parameters.

try this syntax:

Properties props = new Properties();
props.setProperty("user","user");
props.setProperty("password","pass");
c = DriverManager.getConnection(host, props);

hope this helps

MtwStark
  • 3,866
  • 1
  • 18
  • 32
1

I know this question is a bit old now. But i had the same problem and turned out to be the connection METHOD in pg_hba.conf file.

I had the default recommended md5 configuration, but it seems like there is a bug in some versions of the JDBC driver (https://www.postgresql.org/message-id/3E43175C.5020209%40xythos.com)

So i fixed it by simply changing md5 to trust in the pg_hba.conf file, so that it accepts passwords in clear text.

host all all 127.0.0.1/32 trust

hope this helps some one

jambriz
  • 1,273
  • 1
  • 10
  • 25
1

In my case, the problem was trivial but the exception unclear.

The only problem in my connection URL was the DBNAME at the end of the connection. The DB name simply did not exist, was never created and as an end result I was getting this feedback of invalid character.

Make sure your connection settings are actually valid, double check your DB name in the URL. And before attempting to programtically connect to the DB use pgadmin to verify the db is there.

99Sono
  • 3,554
  • 27
  • 39
0

If you are in Windows, you need to use double slashes like:

    String host = "jdbc:postgresql:////localhost:5432//name";
dimirsen Z
  • 873
  • 13
  • 16
0

My solution of this problem:I use application.properties file to connect PostgreSQL, if the username is wrong, and my project has two files then i should change both.

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
aether
  • 21
  • 1
  • How is using an `application.properties` a solution for character encoding issues? I think you are assuming the OP is also using Spring(-Boot) as you do? In addition, you know that any file can be written with any character encoding, so why would that help at all? – Ancoron Apr 03 '19 at 07:38