import java.sql.*; import java.util.*; public class SQLTest06 { private static final String DRIVER_NAME = "com.mysql.jdbc.Driver"; private static final String URL = "jdbc:mysql://localhost:3306/shups?user=shu&password=shu"; private static final byte[] BDATA = { (byte)0x81, // first byte of double-byte SJIS encoding (byte)0x27, // singue quote }; public static void main(String[] args) throws Exception { // Get a connection Class.forName(DRIVER_NAME).newInstance(); Properties props = new Properties(); //props.put("useUnicode", "true"); //props.put("characterEncoding", "sjis"); Connection con = DriverManager.getConnection(URL, props); // Print database and driver metadata DatabaseMetaData meta = con.getMetaData(); System.out.println("=== DatabaseProductVersion : " + meta.getDatabaseProductVersion()); System.out.println("=== DriverVersion() : " + meta.getDriverVersion()); // Define table Statement stmt = con.createStatement(); stmt.executeUpdate("DROP TABLE IF EXISTS blobTable"); stmt.executeUpdate("CREATE TABLE blobTable (field1 BLOB)"); // Insert PreparedStatement pstmt = con.prepareStatement("INSERT INTO blobTable(field1) VALUES(?)"); pstmt.setBytes(1, BDATA); pstmt.executeUpdate(); // Show result ResultSet rs = stmt.executeQuery("SELECT * FROM blobTable"); byte[] result = null; while (rs.next()) { result = rs.getBytes(1); } System.out.println("=== Expected : " + getByteArrayString(BDATA)); System.out.println("=== Result : " + getByteArrayString(result)); // Close things pstmt.close(); stmt.close(); con.close(); } private static String getByteArrayString(byte[] ba) { StringBuffer buffer = new StringBuffer(); if (ba != null) { for (int i = 0; i < ba.length; i++) { buffer.append("0x" + Integer.toHexString(ba[i] & 0xff) + " "); } } else { buffer.append("null"); } return buffer.toString(); } }