Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Insert chinese characters to mysql

843841Mar 11 2005 — edited Mar 13 2005
I have searched the forum and found a useful links --http://forum.java.sun.com/thread.jspa?threadID=530358 about inserting chinese characters to mysql. However, I can't see the chinese characters displayed in the db. The charset and collation of my table are utf8 and utf8_bin respectively. Can anyone help me? Below is my source code

------------------ Servet ---------------------
package test;

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class testServlet extends HttpServlet {


private Connection conn;

public void init(ServletConfig config) throws ServletException {
sSysPhyPath = config.getServletContext().getRealPath("/");
}

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{

// set content type and other response header fields first
res.setContentType("text/html");

// get the communication channel with the requesting client
PrintWriter out = res.getWriter();

out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<body>");

req.setCharacterEncoding("UTF-8");
sValue = req.getParameter("s1");

out.println(sValue.getBytes("UTF-8") + "<br>");

DBConnection objConn = new DBConnection(sSysPhyPath);
try {
conn = objConn.get();
objConn.execQuery(conn, "insert into TBL_test (test_desc) values ('" + sValue.getBytes("UTF-8") + "')");
}
catch(Exception e) {
e.printStackTrace();
}
objConn.closeConnection(conn);

out.println("</body></html>");
}

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
doGet(req, res);
}

public void destroy() {

}
}

--------------- Html ------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body bgcolor="#666666">
<form name="test" method="post" action="testServlet">
<input type="text" name="s1">
<input type="Submit" name="Submit" value="Submit">
</form>
</body>
</html>

Besides, if I code the chinese characters directly in the servlet page, what is the encoding of the chinese characters by default? Is it related to my OS locale?

Thanks a lot.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 10 2005
Added on Mar 11 2005
2 comments
365 views