扩展PreparedStatement,以便输出执⾏的sql 语句,即sql
⽇志
import java.math.BigDecimal;import java.URL;import java.sql.Array;import java.sql.Blob;import java.sql.Clob;import
java.sql.Connection;import java.sql.Date;import java.sql.ParameterMet aData;import java.sql.PreparedState ment;import java.sql.Ref;import java.sql.ResultSet;import java.sql.ResultSetMeta Data;import java.sql.SQLException;import java.sql.SQLWarning;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 mayankai
* */
public class
LoggableStatement implements
= true; Connection conn = null; PreparedStatement pstmt = null; conn = new JdbcTemplate().getCo nnection(); String sql = "lect * from cfr_stat_03 where dept_id =? and stat_type=? and stat_key=?"; if(isLogEnable){ pstmt = new LoggableStatement(co nn,sql); }el{ pstmt = conn.prepareStatemen t(sql); } // pstmt = conn.prepareStatemen t(sql); pstmt.tInt(1, dept_id); pstmt.tInt(2,
stat_type); pstmt.tString(3, stat_key); if(isLogEnable){ System.out.println("Ex ecuting query: "+((LoggableStatement)pstmt).getQueryString ()); } 下⾯把参考⽂章附上: 今天为了在打印sql⽇志时,能够打印出完整的sql⽇志(不带‘?’),测试了下 p6spy ,可以满⾜需求,查看了下⾥⾯的⼤致的源码,主要采⽤了装饰器模式进⾏扩展的。在此做下笔记。 1,p6spy的配置在⽹络上google下。2,Class.forName("ine.spy.P6SpyDriver");进⾏触发该类下静态模块的执⾏,---》这个主要实现了该类下的initMethod P6SpyDriver extends P6SpyDriverCore Java代码1. public abstract class P6SpyDriverCore implements Driver { 2. //由实现的接⼝,以及该声明,就可知其
为decorator 3. protected Driver passthru = null ;
4. 5. public synchronized static void initMethod(String spydriver) { 6. //主要实现为两步,1,对DrvierManager的扩展,2,connect的获取 7. 8. //这⾥只粘出重点的代码 9. 10. if (hasModules) { 11. spy = new P6SpyDriver();
12. //注册进DriverManager,你如果在写连接时不⽤Class.forName()的话,也可以⽤这种设置 13. isterDriver(spy); 14. } 15. 16. //在配置⽂件⾥,读取真的driver 17. Driver realDriver = (Driver)P6Util.forName(className).newInstance(); 18. if (DeregisterDrivers()) { 19. // just in ca you had to deregister
20. isterDriver(realDriver); 21. }
22.
23. // now wrap your realDriver in the spy
带的网名24. if (hasModules) {
25. //设置到以上的参数中
26. spy.tPassthru(realDriver);
27. realDrivers.add(realDriver);
28. }
29. }
30.
31.
32. public Connection connect(String p0, java.util.Properties p1) throws SQLException {
33.
34. Connection conn = t(realUrl,p1);
35.
36. if (conn != null) {
37. //这⾥是对connection的包装,也是⼀个decorator //P6LogConnection implements Connection祝贺词
38. //供Connection()获取,
39. conn = wrapConnection(conn);
40. }
41. return conn;
42. }
43.
44.
45. }
public abstract class P6SpyDriverCore implements Driver {
曹丕儿子//由实现的接⼝,以及该声明,就可知其为decorator
protected Driver passthru = null;
public synchronized static void initMethod(String spydriver) {
//主要实现为两步,1,对DrvierManager的扩展,2,connect的获取
//这⾥只粘出重点的代码
if (hasModules) {
spy = new P6SpyDriver();
//注册进DriverManager,你如果在写连接时不⽤Class.forName()的话,也可以⽤这种设置
}
//在配置⽂件⾥,读取真的driver
Driver realDriver = (Driver)P6Util.forName(className).newInstance();
if (DeregisterDrivers()) {
// just in ca you had to deregister
}
// now wrap your realDriver in the spy
if (hasModules) {
//设置到以上的参数中
spy.tPassthru(realDriver);
realDrivers.add(realDriver);
}
}
public Connection connect(String p0, java.util.Properties p1) throws SQLException {
Connection conn = t(realUrl,p1);
if (conn != null) {
结婚六周年//这⾥是对connection的包装,也是⼀个decorator //P6LogConnection implements Connection
//供Connection()获取,
conn = wrapConnection(conn);
}
return conn;
}
}
3,当Connection(...)时,就会去调⽤以上的connect⽽这个connect也是经过包装的。。
Java代码
1. //现在到了P6Connection.prepareStatement(sql);
2. public class P6Connection extends P6Ba implements java.sql.Connection {
3. protected Connection passthru;
4.
5. //P6PreparedStatement 返回的也是对PreparedStatement的装饰扩展
6. public PreparedStatement prepareStatement(String p0) throws SQLException {
7. return (getP6Factory().getPreparedStatement(passthru.prepareStatement(p0), this, p0));
8. }
9.
10. }
/
/现在到了P6Connection.prepareStatement(sql);
public class P6Connection extends P6Ba implements java.sql.Connection {
protected Connection passthru;
//P6PreparedStatement 返回的也是对PreparedStatement的装饰扩展
public PreparedStatement prepareStatement(String p0) throws SQLException {
山东介绍return (getP6Factory().getPreparedStatement(passthru.prepareStatement(p0), this, p0));
}
}
4,这⼀步到了处理PreparedStatement,以上的扩展为的就是为了驱动时执⾏我们所装饰的P6PreparedStatement
Java代码
1. public class P6PreparedStatement extends P6Statement implements PreparedStatement {
2.雨景图片大全
3.
4. public final static int P6_MAX_FIELDS = 32;
5. public static int P6_GROW_MAX = 32;
6. protected PreparedStatement prepStmtPassthru;
7. protected String preparedQuery;
8. protected Object values[];//扩展就是为了取我们所设置的值
9. protected boolean isString[];
10.
11. public P6PreparedStatement(P6Factory factory, PreparedStatement statement, P6Connection conn, String query) {
12. super(factory, statement, conn);
13. prepStmtPassthru = statement;
14. this.preparedQuery = query;
15. initValues();
16. }
17.
18. protected void initValues() {
成都吉他弹唱19. values = new Object[P6_MAX_FIELDS+1];
20. isString = new boolean[P6_MAX_FIELDS+1];
21. }
22.
23. public void addBatch() throws SQLException {
24. prepStmtPassthru.addBatch();
25. }
26.
27. public void clearParameters() throws SQLException {
28. prepStmtPassthru.clearParameters();
29. }
30.
31. public boolean execute() throws SQLException {
32. ute();
33. }
34.
35. public ResultSet executeQuery() throws SQLException {
36. ResultSet resultSet = uteQuery();
37. return (getP6Factory().getResultSet(resultSet, this, preparedQuery, getQueryFromPreparedStatement()));
38. }
39.
40. public int executeUpdate() throws SQLException {
41. uteUpdate();
42. }
白丝女生43.
44. public ResultSetMetaData getMetaData() throws SQLException {
45. MetaData();
46. }
47.
48. public void tArray(int p0, Array p1) throws SQLException {
49. tObjectAsString(p0, p1);
50. // we need to make sure we get the real object in this ca
51. if (p1 instanceof P6Array) {
52. prepStmtPassthru.tArray(p0,((P6Array)p1).passthru);
53. } el{
54. prepStmtPassthru.tArray(p0,p1);
55. }
56. }
57. //以下设置都为装饰器的扩展,使我们每⼀次设置到?的值都会加到数组中去。
58. public void tAsciiStream(int p0, InputStream p1, int p2) throws SQLException {
59. tObjectAsString(p0, p1);
60. prepStmtPassthru.tAsciiStream(p0,p1,p2);
61. }