📄 stringtoinserttodbfilter.java
字号:
package tools;
/*
* @Author:黄顺武
* Create Time:2008-2-23
* Description:把欲插入到数据库中的字段中的'转化成\huangshunwu19831231并返回转化后的值,避免插入数据库中时发生插入错误,同时在从数据库中读出记录时把\huangshunwu19831231反转化为'
*
*/
public class StringToInsertToDBFilter {
private static String myNameAndBirthday = "huangshunwu19831231";// 自定义的转换常量
public StringToInsertToDBFilter() {
}
public static String doFilter(String sqlStr) {
if (sqlStr != null) {
StringBuffer sqlBuffer = new StringBuffer(sqlStr);
int firstIndex = sqlBuffer.indexOf('\'' + "");
while (firstIndex < sqlBuffer.length() - 1) {
if (firstIndex != -1) {
sqlBuffer.replace(firstIndex, firstIndex + 1,
'\\' + myNameAndBirthday);
firstIndex = sqlBuffer.indexOf('\'' + "", firstIndex
+ myNameAndBirthday.length() + 1);// 沿着替代后的字符串继续向右寻找匹配
} else {
break;
}
}
return sqlBuffer.toString();
} else {
return null;
}
}
public static String doReverseFilter(String sqlStr) {
if (sqlStr != null) {
StringBuffer sqlBuffer = new StringBuffer(sqlStr);
int firstIndex = sqlBuffer.indexOf('\\' + myNameAndBirthday);
while (firstIndex < sqlBuffer.length() - 1) {
if (firstIndex != -1) {
sqlBuffer.replace(firstIndex, firstIndex
+ myNameAndBirthday.length() + 1, '\'' + "");
firstIndex = sqlBuffer.indexOf('\\' + myNameAndBirthday,
firstIndex + 1);// 沿着替代后的字符串继续向右寻找匹配
} else {
break;
}
}
return sqlBuffer.toString();
} else {
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -