連接 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 開頭加上:
<%@ page contentType="text/html; charset=Big5" %>

arrow
arrow
    全站熱搜

    npitt 發表在 痞客邦 留言(0) 人氣()