0

Using Java 8 and a Postgres 10 Server, and the v 42.2.16 Postgresql Driver, I have a basic JDBC query function:

public List<Map<String, Object>> Query(String sql) throws Exception {
    Connection con = null ;
    PreparedStatement pstmt;
    List<Map<String, Object>> resultSetToList = null;
    try {
        
        con = DriverManager.getConnection(ConnectionString, Properties);
        pstmt = con.prepareStatement( sql );
        pstmt.execute();
        
        ResultSet resultSet = pstmt.getResultSet();
        resultSetToList = resultSetToList(resultSet);

        pstmt.close();

    } catch(Exception e){
        throw e;
    }
        finally {
    
        if (con != null)
            con.close();
    }
    return resultSetToList;
}

I execute a query like this:

Query("SELECT * FROM bi_functions");

But it throws an Exception

org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0xa3

The database (provided by 3rd party) has encoding SQL_ASCII. 0xA3 is the 'British Pound' character.

So I change the query to:

SET LOCAL CLIENT_ENCODING TO 'SQL_ASCII'; SELECT * FROM bi_functions;

It fails with:

org.postgresql.util.PSQLException: The server's client_encoding parameter was changed to SQL_ASCII. The JDBC driver requires client_encoding to be UTF8 for correct operation.

Is there a way using plain JDBC to circumvent this error?

2
  • No, you cannot do that. The interesting question is: what is the server encoding, and what bytes are stored in the database? Commented Sep 21, 2020 at 5:50
  • DB (provided by 3rd party) has encoding SQL_ASCII. 0xA3 is the 'British Pound' character. Are you saying I can't read this DB via JDBC? Commented Sep 21, 2020 at 5:59

2 Answers 2

1

Java uses a Unicode encoding internally, so you cannot use a client_encoding different from UTF8 with the JDBC driver.

You should figure out the actual encoding of the database (probably one of the ISO 8859 or a Windows encoding). Then create a database with that encoding and dump the original database and load your dumped database into it.

Otherwise you won't be able to use this database with JDBC.

Sign up to request clarification or add additional context in comments.

5 Comments

On the 3rd party server, the DB is listed as Encoding SQL_ASCII; Collation en_GB; Character Type en_GB. Is there another way to determine the "actual encoding" ? It's a Windows server, so probably 1252 encoding. If I create a DB locally with that encoding, won't I encounter the same issue when querying it via JDBC?
If all the data in the database are consistent with Windows-1252, you won't have a problem. If they are not (say, some UTF-8 data have sneaked in), you'll have to repair the data in the database.
I'm restoring the DB on a Mac (Catalina) server -does that make a difference?
No, the platform shouldn't make a difference.
restoring the backup to a DB created with ENCODING = 'WIN1252' worked, thanks
0

Add ?allowEncodingChanges=true to the connection URL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.