import java.sql.*; import java.util.*; public class testMb4 { static String url = "jdbc:mysql://127.0.0.1:5530/test"; public static void main (String args []) { Connection conn = null; Statement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); Properties props = new Properties(); props.setProperty("user", "msandbox"); props.setProperty("password", "msandbox"); props.setProperty("characterEncoding", "UTF-8"); // props.setProperty("useJvmCharsetConverters", "true"); conn = DriverManager.getConnection(url, props); stmt = conn.createStatement(); ResultSet rs; stmt.execute("DROP TABLE IF EXISTS t99"); stmt.execute("CREATE TABLE t99 (a VARCHAR(10)) CHARACTER SET utf8mb4"); stmt.execute("INSERT INTO t99 VALUES(0xF0AFA6B2)"); rs = stmt.executeQuery("SELECT * FROM t99"); int count = 0; while (rs.next()) { String a = rs.getString("a"); System.out.println("row" + count + ":\t" + a); count++; } count = 0; stmt.execute("SET NAMES utf8mb4"); rs = stmt.executeQuery("SELECT * FROM t99"); while (rs.next()) { String a = rs.getString("a"); System.out.println("row" + count + ":\t" + a); count++; } count = 0; stmt.execute("SET NAMES binary"); rs = stmt.executeQuery("SELECT * FROM t99"); while (rs.next()) { String a = rs.getString("a"); System.out.println("row" + count + ":\t" + a); count++; } rs = stmt.executeQuery("SHOW VARIABLES LIKE 'chara%'"); while (rs.next()) { String name = rs.getString("Variable_name"); String val = rs.getString("Value"); System.out.println(name + ":\t" + val); } } catch (SQLException se) { se.printStackTrace(); System.err.println( "SQLSTATE: " + se.getSQLState() + "\nError Code: " + se.getErrorCode() + "\nError Message: " + se.getMessage() ); } catch (ClassNotFoundException cnfe) { cnfe.printStackTrace(); } finally { try { if(stmt != null) stmt.close(); if(conn != null) conn.close(); } catch(SQLException e) { /* ignore */ } } } }