Showing posts with label mysql_update. Show all posts
Showing posts with label mysql_update. Show all posts

Saturday, November 24, 2012

Insert arabic word to mysql database using java

    Connection con = null;
    String unicode= "?useUnicode=yes&characterEncoding=UTF-8"; 
    String url = "jdbc:mysql://localhost/";
    String db = "students";
    String driver = "com.mysql.jdbc.Driver";
    try {
        Class.forName(driver);
        con = DriverManager.getConnection(url+db+unicode,"root","");
        Statement st = con.createStatement();
        String name = new String(txtName.getText().getBytes(), "UTF-8");
        int val = st.executeUpdate("insert into student(name, roll) "+
            " VALUES('"+name+"','"+txtRoll.getText()+"')");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
 
You have to create your database as a UTF-8 table otherwise no matter what you 
add it will never be able to store it... 


Ensure that your MySQL configuration encoding is defined correctly. Add these lines to either my.cnf "in the case if using linux" or my.ini "case you using windows"
[client] 
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
default-character-set=utf8
character-set-server=utf8