java操作数据库的工具类 (六)

2014-11-24 11:24:38 · 作者: · 浏览: 15
scapeUtils.escapeSql((String) value) + "'";
} else if (value instanceof Number) {
repStr = value.toString();
} else if (value instanceof Timestamp) {
repStr = "'"
+ new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.S")
.format(DateUtils.toDate((Timestamp) value)) + "'";
} else if (value instanceof Date) {
repStr = "'" + new SimpleDateFormat("yyyy/MM/dd").format(value)
+ "'";
} else if (value instanceof List) {
@SuppressWarnings("unchecked")
List list = (List) value;
for (Object o : list) {
repStr += ",";
if (o instanceof String) {
repStr += "'" + StringEscapeUtils.escapeSql((String) o)
+ "'";
} else if (o instanceof Number) {
repStr += o.toString();
} else if (o instanceof Timestamp) {
repStr += "'"
+ new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.S")
.format(DateUtils.toDate((Timestamp) o))
+ "'";
} else if (o instanceof Date) {
repStr += "'"
+ new SimpleDateFormat("yyyy/MM/dd").format(o)
+ "'";
}
}
if (repStr.length() > 0) {
repStr = repStr.substring(1);
} else {
repStr = "''";
}
} else {
repStr = "null";
}

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(this.sqlQry + ")");
boolean matchFound = matcher.find();
StringBuffer strBuffer = new StringBuffer(this.sqlQry);
int position = 0;

while (matchFound) {
for (int i = 0; i <= matcher.groupCount(); i++) {
String groupStr = matcher.group(i);

strBuffer.replace(position + matcher.start(), position
+ matcher.start() + groupStr.length() - 1, repStr);
position += repStr.length() - (groupStr.length() - 1);
}

if (matcher.end() + 1 > this.sqlQry.length())
break;
matchFound = matcher.find(matcher.end());
}

this.sqlQry = strBuffer.toString();
}

/**
* 启用事务
*/
public void beginTrans() {
helper.beginTrans();
log.debug("事务已经启用, 请在交易完成时调用closeTrans");
}

/**
* 结束事务并提交
*/
public void closeTrans() {
helper.closeTrans();
}
}