package com.zhh.function.util;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
/**
*
* 项目名称:CFR
* 类名称:LoggableStatement
* 类描述:扩展PreparedStatement,以便输出执行的sql语句,即sql日志
* 创建时间:2010-6-22 下午10:47:39
* @version 1.0
* @author zhh
*来自网络 *
*/
public class LoggableStatement implements PreparedStatement {
/** used for storing parameter values needed for producing log */
private ArrayList parameterValues;
/** the query string with question marks as parameter placeholders */
private String sqlTemplate;
/** a statement created from a real database connection */
private PreparedStatement wrappedStatement;
public LoggableStatement(Connection connection, String sql)
throws SQLException {
// use connection to make a prepared statement
wrappedStatement = connection.prepareStatement(sql);
sqlTemplate = sql;
parameterValues = new ArrayList();
}
private void saveQueryParamValue(int position, Object obj) {
String strValue;
if (obj instanceof String || obj instanceof Date) {
// if we have a String, include '' in the saved value
strValue = "'" + obj + "'";
} else {
if (obj == null) {
// convert null to the string null
strValue = "null";
} else {
// unknown object (includes all Numbers), just call toString
strValue = obj.toString();
}
}
// if we are setting a position larger than current size of
// parameterValues, first make it larger
while (position >= parameterValues.size()) {
parameterValues.add(null);
}
// save the parameter
parameterValues.set(position, strValue);
}
// 这一步是对ArrayList与sql进行处理,输出完整的sql语句
public String getQueryString() {
int len = sqlTemplate.length();
StringBuffer t = new StringBuffer(len * 2);
if (parameterValues != null) {
int i = 1, limit = 0, base = 0;
while ((limit = sqlTemplate.indexOf('?', limit)) != -1) {
t.append(sqlTemplate.substring(base, limit));
t.append(parameterValues.get(i));
i++;
limit++;
base = limit;
}
if (base
t.append(sqlTemplate.substring(base));
}
}
return t.toString();
}
public void addBatch() throws SQLException {
wrappedStatement.addBatch();
}
public void clearParameters() throws SQLException {
wrappedStatement.clearParameters();
}
public boolean execute() throws SQLException {
return wrappedStatement.execute();
}
public ResultSet executeQuery() throws SQLExcepti