I am trying to store UTF-8 encoded text with MM.MySQL 2.0.13 driver.  I am 
using MySQL 3.23.49, MM.MySQL 2.0.13 JDBC driver, j2sdk1.4.0.

I have had success with the code below (which stores the UTF-8 as a binary 
stream), but am wondering if there is an easier way.  Specifically I am 
wondering about the MM.MySQL useUnicode and characterEncoding params.  The 
code below works regardless how you set these params.

Do useUnicode and characterEncoding allow you to store UTF-8 with regular 
.setString() methods?  What exactly do they do?  I can't find any 
explanation more than:

useUnicode      should the driver use Unicode character encodings when handling 
strings? (true/false)   false
characterEncoding       if useUnicode is true, what character encoding should the 
driver use when dealing with strings?   none


Is setBinaryStream() the only way to store UTF-8?

Thank you very much in advance!!!

John D. Stein

// Store a unicode character (u) in the database and see if what you get
// back from the database (urc) equals the original string you put in.

public void testBinaryUTF8() throws Exception {

     String u = "\u65E5";
     byte[] ua = u.getBytes("UTF-8");
     ByteArrayInputStream is = new ByteArrayInputStream(ua);
     String updateSQL = "UPDATE sp_i18n_dev.lengthtest SET n255 = ? WHERE 
id = 1";
     String selectSQL = "SELECT n255 from sp_i18n_dev.lengthtest WHERE id = 
1";

     Connection conn = DataSource.getConn(dsName);

     PreparedStatement pst = conn.prepareStatement(updateSQL);
     pst.setBinaryStream(1, is, ua.length);
     pst.execute();

     pst = conn.prepareStatement(selectSQL);
     ResultSet rs = pst.executeQuery();
     rs.next();

     BufferedReader brin = new BufferedReader(new 
InputStreamReader(rs.getBinaryStream(1), "UTF-8"));
     String urc = brin.readLine();

     System.out.println(
         "Original string equals string from db: " +
         u.equals(urc));

     DataSource.cleanup(conn, dsName, pst, rs);
}


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to