简体   繁体   中英

Illegal UTF-8 sequence connecting with postgreSQL database

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

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

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

如果您在Windows中,则需要使用双斜杠,例如:

    String host = "jdbc:postgresql:////localhost:5432//name";

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.

我对这个问题的解决方案:我使用application.properties文件连接PostgreSQL ,如果用户名错误,我的项目有两个文件,那么我应该改变它们。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM