> -----Original Message----- > From: michael_wu[吳宏達] > Sent: Wednesday, May 26, 2004 9:12 AM > To: [EMAIL PROTECTED] > Subject: Re: FW: create database in specified character set > > Hi Hassan again, > Thanks for your kindly support. My program now works. I post > the source code here so that the others who run into the same > problem can refer to it. Hope it helps! > Regards, > /** > * <p>Title: testMySQL411.java</p> > * <p>Description: This is a simple programming exercise to get to know > MySQLs' unicode support </p> > * @author Michael Wu ([EMAIL PROTECTED]) > */ > > import java.io.*; > import java.sql.*; > > public class testMySQL411 { > private static final String dbUser = "root"; > private static final String dbPwd = ""; > private static final String dbSvr = "localhost"; > /* I created this database with the following SQL command: > CREATE DATABASE trdb CHARACTER SET utf8; > */ > private static final String dbSrc = "trdb"; > > public static void main(String argv[]) { > try { > String jdbcDriver = "com.mysql.jdbc.Driver"; > Class.forName(jdbcDriver).newInstance(); > // the connect url must specify > useUnicode=true&characterEncoding=UTF-8; > // specify characterEncoding=UTF-8 alone won't work > Connection conn = > DriverManager.getConnection("jdbc:mysql://"+dbSvr+"/"+dbSrc+ > > "?useUnicode=true&characterEncoding=UTF-8"/*required*/, > dbUser, dbPwd); > conn.setAutoCommit(false); > // dump the current content of the table > Statement stmt = conn.createStatement(); > ResultSet rs = stmt.executeQuery("select * from customer"); > while (rs.next()) { > String nameout = rs.getString("Name"); > System.out.println(nameout); > int age = rs.getInt("Age"); > System.out.println(age); > } > rs.close(); > rs = null; > int count = stmt.executeUpdate("delete from customer"); > System.out.println("Count="+count); > stmt.close(); > stmt = null; > String name = "a小孩"; // a string with traditional chinese > characters; > // replace it with a string which consists of characters in your > local language > PreparedStatement pstmt = conn.prepareStatement("insert into > customer(name, age) values(?,?)"); > pstmt.setString(1, name); > pstmt.setInt(2, 38); > pstmt.executeUpdate(); > conn.commit(); > pstmt.close(); > pstmt = conn.prepareStatement("select name from customer"); > rs = pstmt.executeQuery(); > while (rs.next()) { > String nameout = rs.getString("name"); > System.out.println(nameout); > } > conn.close(); > } catch( Exception ex ) { > System.out.println(ex.getMessage()); > ex.printStackTrace(); > } > System.out.println("+---------- END of simple unicode support Test > ------------+\n"); > } > } > > michael_wu[吳宏達] wrote: > > > Many thanks! I had downloaded V4.1.1 and created a database in utf8 > > character set. > > I successfully created a table and inserted rows of data with VARCHAR > > columns into the table through the SQL pad. > > With the Control Center GUI, I can see the result. It seems the data in > the > > table are right. I can see the multiple byte > > characters in the varchar columns. However, when my application > selected > > data through JDBC, the returned strings are wrong. > > Looks the returned strings are not in the right character set. > > How are you displaying the data returned via JDBC? As a JSP page > or in a Java GUI (Swing/AWT) app? > > If the former, servlet containers default to ISO-8859-1 encoding... > > What version of Connector/J are you using, and are you specifying > Unicode and charset in your connection URL, e.g. > > jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8 > > -- > Hassan Schroeder ----------------------------- [EMAIL PROTECTED] > Webtuitive Design === (+1) 408-938-0567 === http://webtuitive.com > > dream. code. >
-- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/[EMAIL PROTECTED]