java.io.IOException: Illegal UTF-8 sequence: initi

2019-09-18 02:36发布

问题:

I have followed the following tutorial:

http://examples.javacodegeeks.com/core-java/java-postgresql-example/

org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:257)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:159)
at org.postgresql.Driver.makeConnection(Driver.java:415)
at org.postgresql.Driver.connect(Driver.java:283)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.javacodegeeks.examples.TestConnection.main(TestConnection.java:16)
Caused by: java.io.IOException: Illegal UTF-8 sequence: initial byte is 11111xxx: 252
at org.postgresql.core.UTF8Encoding.decode(UTF8Encoding.java:125)
at org.postgresql.core.PGStream.ReceiveString(PGStream.java:329)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:424)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:203)
... 7 more

I am using Eclipse and have added the latest PostgreSQL JDBC driver to my project (renamed to postgres.jar), my code is as shown in the tutorial.

PostgreSQL Server is running I believe, I am using Windows 10 and have run "pg_ctl start -D ", the path is correct and the server seems to be working as far as I can tell

I have checked in pgAdmin and the database encoding is UTF8.

Can someone help please?

回答1:

I had the same issue. It was solved by setting a password for the user I was trying to connect with. An older version of the JDBC driver provided a useful error message.



回答2:

Also had this problem. In my case there was broken connection string, so check if the format of your connection string is correct:

spring.datasource.url=jdbc:postgresql://localhost:5432/my-database