Java – illegal UTF-8 sequence connected to PostgreSQL database
•
Java
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"); }
I received 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 intentodeconexi ó nfall ó" means "connection attempt failed"
Please help me, I don't know what to do
Editor: I also checked the server code and it said it was utf8
Solution
I think the problem may be that the Java string is utf-16 and PostgreSQL requires UTF-8 parameters
Try this syntax:
Properties props = new Properties(); props.setProperty("user","user"); props.setProperty("password","pass"); c = DriverManager.getConnection(host,props);
I hope this can help
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码