java – 与postgreSQL数据库连接的非法UTF-8序列

前端之家收集整理的这篇文章主要介绍了java – 与postgreSQL数据库连接的非法UTF-8序列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码连接到数据库
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");
}

我收到以下错误

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ó”表示“连接尝试失败”.

请帮帮我,我不知道该怎么办.

编辑:我还检查了服务器编码,它说它是UTF8

解决方法

我认为问题可能是java字符串是UTF-16而Postgresql需要UTF-8参数.

试试这个语法:

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

希望这可以帮助

猜你在找的Java相关文章