Re: Storing UTF-8 text with MM.MySQL 2.0.13 driver.

2002-05-20 Thread Ronald Muller

This code works fine, which shows you can use the useUnicode and
characterEncoding properties with simple Statement's:

Properties prop = new Properties();
Class.forName(org.gjt.mm.mysql.Driver);
// true as String and not: Boolean.valueOf(true)!
prop.put(useUnicode, true);
prop.put(characterEncoding, UTF-8);
con = DriverManager.getConnection(dbUrl, prop);
String s = \u20AC (euro) \u2126 (ohm);
Statement stm = con.createStatement();
stm.executeUpdate(DROP TABLE IF EXISTS TestUnicode);
stm.executeUpdate(CREATE TABLE TestUnicode (code VARCHAR(128)));
stm.executeUpdate(INSERT INTO TestUnicode (code) VALUES (\'+s+\'));
ResultSet rs = stm.executeQuery(SELECT code FROM TestUnicode);
if (!rs.next()) {
 System.err.println(No row?!);
} else {
 System.out.println(s.equals(rs.getString(code)) ? OK : FAILED);
}
...

The downside is that the stored String does NOT appear correct in the
SQL tools (for instance MySqlManager). I *think* the problem is that
MySQL is not using UNICODE. 
Is there a workaround for this issue??

Ronald


-
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




Storing UTF-8 text with MM.MySQL 2.0.13 driver.

2002-05-18 Thread John D. Stein

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




Re: Storing UTF-8 text with MM.MySQL 2.0.13 driver.

2002-05-18 Thread Mark Matthews

You would need to put useUnicode=truecharacterEncoding=UTF-8 in your JDBC
url, like the README directs. See
http://java.sun.com/j2se/1.4/docs/guide/intl/encoding.doc.html for a list of
supported encodings.

-mark

- Original Message -
From: John D. Stein [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Saturday, May 18, 2002 10:43 AM
Subject: Storing UTF-8 text with MM.MySQL 2.0.13 driver.


 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




-
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