連接 Mysql Database Server:
-------------------------------------------------------------------------------
mysql 不支持 unicode,所以比較麻煩。
先將 URL String 設置成 encoding 為 Big5
String url = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5";
Insert to DB 測試代碼:(須編碼)
String str = "中文字";
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO test VALUES (?)");
pStmt.setString(1,new String(str.getBytes("ISO8859_1"),"Big5");
pStmt.executeUpdate();
Fetch from DB 測試代碼:(不需再一次編碼)
try {
PreparedStatement pStmt = conn.prepareStatement("SELECT CODE FROM TEST");
pstmt = connection.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
CODE = rs.getString("CODE");
}catch(SQLException se){}
Servlet
-------------------------------------------------------------------------------
在 Servlet 開頭加上:
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
JSP
-------------------------------------------------------------------------------
在 JSP 開頭加上:
- Jan 21 Fri 2005 14:26
Java 使用 Mysql 時的中文處理
全站熱搜
留言列表
禁止留言