Storing the chinese characters exactly in to mysql db .....Its urgent 4 me
hi
Thanx alot for every one.......
Here iam struggleling from last few weeks with this problem
That goes like this
How to insert a chinese character exctly in to the mysql data base(Iam using mysql-5.0)
Here i did some thing......please can you correct it if anything goes wrong..
Some where in the doc's i have searched in the net they are specified that change the default char set of client and server in my.ini
According to them here iam changed to utf8 in both places (client and server)
This file avilable in mysql root directory...
then iam
created database with chatset utf8
created a table with chatset utf8 and
specifing column with charset utf8 and collation utf8_general_ci
then
When i submitting the form the data which iam passing is converted to NCR (Like &#220027). formated string..this string iam getting in action class.here iam exactly strucked what should i with this string to connver to chinese character...Please give me what sort of steps sholud i follow here up to DB level ..
I kown that iam doing some thing wrong with my database configuration. please give me some explanation(if possible Example) how can i setup my database to set these chinese characters...
here iam specifying my db settings:
mysql> SHOW VARIABLES LIKE 'char%';
+--+-
| Variable_name| Value
+--+-
character_set_client| utf8
|
| character_set_connection | utf8
|
| character_set_database| utf8
|
| character_set_results| utf8
|
| character_set_server| utf8
|
| character_set_system| utf8
|
| character_sets_dir| C:\Program Files\MySQL\MySQL Server 5.0\share\chars
ets\ |
+--+-
--+
mysql> SHOW VARIABLES LIKE 'col%';
+-+--+
| Variable_name| Value|
+-+--+
| collation_connection | utf8_general_ci |
| collation_database| utf8_general_ci |
| collation_server| utf8_general_ci |
+-+--+
Please let me know if iam doing any thing wrong....
iam using jdbc to connect to the db..Here is my simple java code...
public class StroreChinese2DBUsingJdbc
{
public static void main(String args[]){
try {
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost:3306/test?user=root&password=admin&databaseName=test&integratedSecurity=true&useUnicode=true&characterEncoding=utf8";
Connection con = null;
String str="件";
con = DriverManager.getConnection(connectionUrl);
str=StoreChineseChar2DBServlet.NCR2UnicodeString(str);
PreparedStatement ps1 = con.prepareStatement("insert into big5 values(?)");
ps1.setString(1, str);
int noofRows = ps1.executeUpdate();
System.out.println("No of rows effected::::"+noofRows);
PreparedStatement ps2 = con.prepareStatement("select * from big5");
ResultSet rs2=ps2.executeQuery();
while(rs2.next()){
System.out.println("effected data::::"+rs2.getString(1));
}
rs2.close();
ps1.close();
ps2.close();
con.close();
}catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} /*catch (UnsupportedEncodingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} */catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
public static String NCR2UnicodeString(String str)
{
StringBuffer ostr = new StringBuffer();
int i1=0;
int i2=0;
while(i2<str.length())
{
i1 = str.indexOf("&#",i2);
if (i1 == -1 ) {
ostr.append(str.substring(i2, str.length()));
break ;
}
ostr.append(str.substring(i2, i1));
i2 = str.indexOf(";", i1);
if (i2 == -1 ) {
ostr.append(str.substring(i1, str.length()));
break ;
}
String tok = str.substring(i1+2, i2);
try {
int radix = 10 ;
if (tok.trim().charAt(0) == 'x') {
radix = 16 ;
tok = tok.substring(1,tok.length());
}
ostr.append((char) Integer.parseInt(tok, radix));
} catch (NumberFormatException exp) {
ostr.append('?') ;
}
i2++ ;
}
return new String(ostr) ;
}
}
With this code it is stroring some junk value in side the db instead of chinese characters..
Iam very much thank full to every one ..iam really suffering with this from last few weeks...Please letme know is there any thing wrong in this programming ..
If the explanation in not proper please let know iam ready to explain.......again and again this is some what ungent for me...
Thanking you
Srikanth>

