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 VALUES (?)"; 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 VALUES (?)"; 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" %>
|