Re: Per field encoding
Posted by: Tom Eugelink
Date: May 10, 2024 07:17AM

Yes. I understand. But: both characterEncoding and clobCharacterEncoding are set to latin1, the table has UTF8. Writing a string using setString works, writing a string using setCharacterStream does not. There appears to be a difference in how these two handle table level encoding.

I have discovered that this system still uses MySQL 5.7 (JDBC driver 8.2.0).

class Scratch {

public static void main(String[] args) throws SQLException {
Properties properties = new Properties();
properties.put("user", "...");
properties.put("password", "...");
properties.put("characterEncoding", "latin1");
properties.put("clobCharacterEncoding", "latin1");
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/mydb", properties);
connection.setAutoCommit(true);

String s = "Tëst"; // note the ë
{
PreparedStatement preparedStatement = connection.prepareStatement("update utf8table set message = ?");
preparedStatement.setString(1, s);
preparedStatement.executeUpdate();
System.out.println("setString");
}
{
PreparedStatement preparedStatement = connection.prepareStatement("update utf8table set message = ?");
preparedStatement.setCharacterStream(1, new StringReader(s));
preparedStatement.executeUpdate();
System.out.println("setCharacterStream");
}
}
}

Results in:

setString
Exception in thread "main" java.sql.SQLException: Incorrect string value: '\xEBst' for column 'message' at row 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:130)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:912)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1054)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1003)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1312)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:988)
at Scratch.main(scratch.java:30)

Options: ReplyQuote


Subject
Written By
Posted
May 02, 2024 05:39AM
May 03, 2024 05:02AM
May 03, 2024 06:46AM
May 06, 2024 03:58AM
May 06, 2024 04:23PM
Re: Per field encoding
May 10, 2024 07:17AM
May 10, 2024 10:58AM
May 10, 2024 11:03AM
May 13, 2024 06:51PM
May 13, 2024 11:56PM


Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.