#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    86
    Rep Power
    10

    Cool JSP+mySQL: insert chinese character into database through JSP


    hi,

    i have problem with insert a chinese value in jsp ...
    e.g.
    i try to run this query "insert into abc values ('是')"...
    it is a successful opertation, but viewing the inserted value in mySQL Browser, the value shown in the table is '?'...
    but however i try to run the same in the mySQL browser rather through the jsp i'm coding... the value is saved is in the correct format ...

    i have configure the table 'abc' to charset 'gb2312' ....

    i using :
    - mySQL database
    - mysql-connector-java-3.1.5-gamma-bin.jar
    - Sun One Studio 4 (JSP coding)
    - WinXP OS

    please reply asap....
    thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    86
    Rep Power
    10

    Cool


    Code:
    <%@page contentType="text/html; charset=utf-8"%>
    <%@ page import="java.sql.*" %>
    <jsp:useBean id="DBconn" class="beans.DBConn"/>
    <html>
    <head><title>chinese</title></head>
    <body>
    
    <%  //  http://www.hongliang.net/showArt.php?id=91
        /*  1) in DBConn beans, change this :
               DriverManager.getConnection("jdbc:mysql://localhost:3306/ST2301_04S2_01?useUnicode=true&characterEncoding=gb2312","ST2301_04S2_01","12345678");
            2) in the mySQLbrowser, run this query :
               ALTER DATABASE ST2301_04S2_01 DEFAULT CHARACTER SET gb2312 COLLATE gb2312_chinese_ci;
    
            code to note in this jsp page:
            - @page contentType="text/html; charset=utf-8" --> for the displaying of chinese character on the page
            - new String(dataStr.getBytes("ISO-8859-1"),"UTF-8") --> for updating into the database
    */
    
        String dataStr = request.getParameter("data");
        if(dataStr!=null) {
            dataStr = new String(dataStr.getBytes("ISO-8859-1"),"UTF-8"); 
            String query = "INSERT INTO testing VALUES('" + dataStr + "')";
            out.println(query+"<br>");
            out.println(DBconn.connect(query,""));
            DBconn.close("");
        }
    
        String query = "SELECT * FROM testing";
        String errDBStr = DBconn.connect(query,"S");
        ResultSet rst = null;
        if (errDBStr.equals("")) {
            try{
                rst = DBconn.getMyResult();
                while(rst.next()) {
                String tmpStr = rst.getString(1);
                out.println( tmpStr + "<br>"); 
              }
            }catch (Exception exc) { out.println(exc.toString()); }
        } else {  out.println("Error<br>'" + errDBStr + "'");  }
        DBconn.close("S");
    %>
    
    <form>
        <input type=text name=data value="">
        <input type=submit>
    </form>
    
    </body>
    </html>

    Comments on this post

    • pabloj agrees : Nice!!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    1
    Rep Power
    0

    Thanks for the example code


    Thanks so much for the example code. I've been trying for months to figure out how to simply create and send chinese in a form and display it in another jsp page. I would of never guessed to put ISO-589 or whatever it is for the getBytes() parameter. I kept trying to put Big5 and GB...something for simplified. Again thanks.

IMN logo majestic logo threadwatch logo seochat tools logo