Java 中文问题的解决 mysql, oracle, servlet, jspJava 中文问题的解决 mysql, oracle, servlet, jsp:小鱼之家,,编程技术,保险
Java 中文问题的解决 mysql, oracle, servlet, jsp
  Java 中文问题一直困扰许多学习者。总结了下面的一些情况的解决方法。
  希望对大家有帮助。
  连接 Mysql Database Server:
  ------------------------------------------------------------------------------
   mysql 不支持 unicode,所以比较麻烦。
   将 connectionString 设置成 encoding 为 gb2312

   String connectionString
   = jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gb2312 ;
  测试代码:
   String str = 汉字 ;
   PreparedStatement pStmt = conn.prepareStatement( INSERT INTO test
   pStmt.setString(1,str);
   pStmt.executeUpdate();
  数据库表格:
   create table test (
   name char(10)
   )
  连接 Oracle Database Server
  ------------------------------------------------------------------------------
   在把汉字字符串插入数据库前做如下转换操作:
   String(str.getBytes( ISO8859_1 ), gb2312 )

  测试代码:
   String str = 汉字 ;
   PreparedStatement pStmt = conn.prepareStatement( INSERT INTO test
   pStmt.setString(1,new String(str.getBytes( ISO8859_1 ), gb2312 );
   pStmt.executeUpdate();
  Servlet
  ------------------------------------------------------------------------------
   在 Servlet 开头加上两句话:
   response.setContentType( text/html;charset=UTF-8 );
   request.setCharacterEncoding( UTF-8 );
  JSP
  ------------------------------------------------------------------------------
   在 JSP 开头加上:    <%@ page contentType=text/html; charset=gb2312 %>