JDBC: Issue with rendering Danish specific characters: Ø,Å,Æ

Tags: , , ,



Problem: Danish specific characters are displayed improperly(as non readable chars)

Given: Database is Microsoft SQL server 2019, database collation is: Danish_Greenlandic_100_CS_AS. Data has been stored in Varchar column types, data has been already there; change collation or any data modification is not appropriate. I am not inserting data, only reading them(kind of report functionality). Everything is working fine on my local machine, however on staging environment Danish characters are encoded improperly.

Connection initialization snippet

Properties props = new Properties();
props.put ("charSet", "UTF-8");
props.put("username", "dbusername");
props.put("password", "secret");
try (Connection con = getConnection(repositoryUrl, props);
   PreparedStatement preparedStatement = con.prepareStatement(sqlQuery)
) {

Reading data snippet:

ResultSet rs = //getResultSet
while (rs.next()) {
            Map<String, String> rowEntry = new LinkedHashMap<>();
            for (int i =0; i < columnCount; i++ ) {
                int columnIndex = i+1;
                String value = rs.getString(columnIndex);
                rowEntry.put(columnName, value);
            }
            result.add(rowEntry);
        }

Answer

The problem can be related with the use of different charsets for the in MacOS (by default, UTF-8), in your local laptop, and Windows (cp-1252 or some variant) in the staging environment.

Although you indicated charset UTF-8 in you connection properties, perhaps the problem is related with some kind of post processing.

In fact, as indicated in the question comments, both the server response and log files are typical examples of the aforementioned problem with the charset.

Please, review the charset which is in place in the JVM in every environment: review and adapt if necessary the value of the file.encoding system property on your laptop and the staging environment, maybe it will give you some insights about the problem and help you solve it.

So in oder to fix your issue try to add file.encoding JVM parameter while running your application:

java -Dfile.encoding=UTF=8 -jar your_app.jar



Source: stackoverflow