@@ -13160,4 +13160,94 @@ public <T extends Resultset> T preProcess(Supplier<String> sql, Query intercepte
13160
13160
return super.preProcess(sql, interceptedQuery);
13161
13161
}
13162
13162
}
13163
+
13164
+ /**
13165
+ * Tests fix for Bug#34558945, PS using setCharacterStream() fails with "Incorrect string value" if the Java program encoding is not UTF-8 after 8.0.29.
13166
+ *
13167
+ * @throws Exception
13168
+ */
13169
+ @Test
13170
+ void testBug34558945() throws Exception {
13171
+ createTable("testBug34558945Utf8", "(txt VARCHAR(100) COLLATE utf8mb4_0900_ai_ci)");
13172
+ createTable("testBug34558945SJis", "(txt VARCHAR(100) COLLATE sjis_japanese_ci)");
13173
+
13174
+ Properties props = new Properties();
13175
+ props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name());
13176
+ props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
13177
+
13178
+ final String utf8 = "UTF-8";
13179
+ final String sjis = "Shift_JIS";
13180
+ final String[] charEncs = { null, utf8, sjis };
13181
+ final String value = "\u7ADC";
13182
+
13183
+ for (String ce : charEncs) {
13184
+ for (String cce : charEncs) {
13185
+
13186
+ if (ce == null) {
13187
+ props.remove(PropertyKey.characterEncoding.getKeyName());
13188
+ } else {
13189
+ props.setProperty(PropertyKey.characterEncoding.getKeyName(), ce);
13190
+ }
13191
+ if (cce == null) {
13192
+ props.remove(PropertyKey.clobCharacterEncoding.getKeyName());
13193
+ } else {
13194
+ props.setProperty(PropertyKey.clobCharacterEncoding.getKeyName(), cce);
13195
+ }
13196
+
13197
+ boolean useSPS = false;
13198
+ do {
13199
+ props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), Boolean.toString(useSPS));
13200
+ try (Connection testConn = getConnectionWithProps(props)) {
13201
+ int records;
13202
+ PreparedStatement testPStmt;
13203
+ Statement testStmt;
13204
+
13205
+ // Table with UTF-8 column.
13206
+ records = 1;
13207
+ testPStmt = testConn.prepareStatement("INSERT INTO testBug34558945Utf8 VALUES (?)");
13208
+ testPStmt.setString(1, value);
13209
+ assertEquals(1, testPStmt.executeUpdate());
13210
+ testPStmt.setCharacterStream(1, new StringReader(value), value.length());
13211
+ if (sjis.equals(cce) || cce == null && sjis.equals(ce)) {
13212
+ assertThrows(SQLException.class, ".*Incorrect string value:.*", testPStmt::executeUpdate);
13213
+ } else {
13214
+ assertEquals(1, testPStmt.executeUpdate());
13215
+ records = 2;
13216
+ }
13217
+
13218
+ testStmt = testConn.createStatement();
13219
+ this.rs = testStmt.executeQuery("SELECT * FROM testBug34558945Utf8");
13220
+ while (records-- > 0) {
13221
+ assertTrue(this.rs.next());
13222
+ assertEquals(value, this.rs.getString(1));
13223
+ }
13224
+ assertFalse(this.rs.next());
13225
+ testStmt.executeUpdate("TRUNCATE TABLE testBug34558945Utf8");
13226
+
13227
+ // Table with Shift_JIS column.
13228
+ records = 1;
13229
+ testPStmt = testConn.prepareStatement("INSERT INTO testBug34558945SJis VALUES (?)");
13230
+ testPStmt.setString(1, value);
13231
+ assertEquals(1, testPStmt.executeUpdate());
13232
+ testPStmt.setCharacterStream(1, new StringReader(value), value.length());
13233
+ if (sjis.equals(cce) || cce == null && sjis.equals(ce)) {
13234
+ assertEquals(1, testPStmt.executeUpdate());
13235
+ records = 2;
13236
+ } else {
13237
+ assertThrows(SQLException.class, ".*Incorrect string value:.*", testPStmt::executeUpdate);
13238
+ }
13239
+
13240
+ testStmt = testConn.createStatement();
13241
+ this.rs = testStmt.executeQuery("SELECT * FROM testBug34558945SJis");
13242
+ while (records-- > 0) {
13243
+ assertTrue(this.rs.next());
13244
+ assertEquals(value, this.rs.getString(1));
13245
+ }
13246
+ assertFalse(this.rs.next());
13247
+ testStmt.executeUpdate("TRUNCATE TABLE testBug34558945SJis");
13248
+ }
13249
+ } while (useSPS = !useSPS);
13250
+ }
13251
+ }
13252
+ }
13163
13253
}
0 commit comments