mysql+特殊字符+输出,Mysql 特殊字符处置

Mysql 特殊字符处理

import java.util.HashMap;

import java.util.Map;

/**

* User: pdc738 Date: 06.22, 2010 Time: 3:36:20 PM

* To Encoder special character.

* If you want to insert binary data into a string column (such as a BLOB column), the following characters must be represented by escape sequences.

NUL NUL byte (0x00). Represent this character by “\0” (a backslash followed by an ASCII “0” character).

\ Backslash (ASCII 92). Represent this character by “\\”.

' Single quote (ASCII 39). Represent this character by “\'”.

" Double quote (ASCII 34). Represent this character by “\"”.

* Within a string, certain sequences have special meaning unless the NO_BACKSLASH_ESCAPES SQL mode is enabled. Each of these sequences begins with

a backslash (“\”), known as the escape character. MySQL recognizes the following escape sequences in referencesMap

*/

public class MYSQLEncoder {

private static Map referencesMap = new HashMap();

static{

referencesMap.put("'","\\'");

referencesMap.put("\"","\\\"");

referencesMap.put("\\","\\\\");

referencesMap.put("\n","\\\n");

referencesMap.put("\0","\\\0");

referencesMap.put("\b","\\\b");

referencesMap.put("\r","\\\r");

referencesMap.put("\t","\\\t");

referencesMap.put("\f","\\\f");

}

//escape sql tag with the source code.

public static String encode(String source) {

if (source == null)

return "";

StringBuffer sbuffer = new StringBuffer(source.length());

for (int i = 0; i < source.length(); i++) {

String c = source.substring(i,i+1);

//System.out.println("c=" + c);

//System.out.println(referencesMap.get(c));

if (referencesMap.get(c) != null) {

sbuffer.append(referencesMap.get(c));

} else {

sbuffer.append(c);

}

}

return sbuffer.toString();

}

public static void main(String[] args){

String tt = encode("They'are \"ok\". \\a");

System.out.print(tt);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值