1 pjaol 1.1
2 /**
3 * Title: <p>
4 * Description: <p>
5 * Copyright: Copyright (c) Troy Thompson, Bob Byron<p>
6 * Company: JavaUnderground<p>
7 * @author Troy Thompson, Bob Byron
8 * @version 1.1
9 */
10 package com.javaunderground.jdbc;
11
12 import java.sql.*;
13
14 public class StatementFactory {
15
16 /* Default debug level */
17 private static DebugLevel defaultDebug = DebugLevel.OFF;
18
19 /* Default sql formatter */
20 private static SqlFormatter defaultFormatter = new DefaultSqlFormatter();
21
22 pjaol 1.1 /**
23 * StatementFactory returns either a regular PreparedStatement or a DebuggableStatement
24 * class depending on the DebugLevel. If DebugLevel is OFF then a PreparedStatement is
25 * returned. If DebugLevel is ON or VERBOSE then a DebuggableStatement is returned. This
26 * minimizes overhead when debugging is not needed without effecting the code.
27 */
28 public StatementFactory() {
29 }
30
31 /**
32 * Use this method if you want a class to override the global nature of a
33 * property file approach. This gives a class an option of a formatter and
34 * the debug value other than the global setting.
35 * @param con Connection to jdbc data source.
36 * @param stmt sql statement that will be executed.
37 * @param formatter SqlFormatter that matches the database type (i.e. OracleFormatter)
38 * @param debug sets the debug level for this statement. DebugLevel can be OFF, ON, VERBOSE
39 * @return PreparedStatement returns a DebuggableStatement if debug = ON or VERBOSE. Returns a standard
40 * PreparedStatement if debug = OFF.
41 * @exception SQLException thrown if problem with connection.
42 */
43 pjaol 1.1 public static PreparedStatement getStatement(Connection con, String stmt,
44 SqlFormatter formatter, DebugLevel debug) throws SQLException{
45 if (con == null)
46 throw new SQLException("Connection passed to StatementFactory is null");
47 if(debug != DebugLevel.OFF){
48 return new DebuggableStatement(con,stmt,formatter,debug);
49 }else{
50 return con.prepareStatement(stmt);
51 }
52 }
53
54 /**
55 * Use this if you want a class to override the global nature of a property
56 * file approach. This gives a class an option of a formatter other than the global setting.
57 * @param con Connection to jdbc data source.
58 * @param stmt sql statement that will be executed.
59 * @param formatter SqlFormatter that matches the database type (i.e. OracleFormatter)
60 * @return PreparedStatement returns a DebuggableStatement if debug = ON or VERBOSE. Returns a standard
61 * PreparedStatement if debug = OFF.
62 * @exception SQLException thrown if problem with connection.
63 */
64 pjaol 1.1 public static PreparedStatement getStatement(Connection con, String stmt,
65 SqlFormatter formatter) throws SQLException{
66
67 return StatementFactory.getStatement(con,stmt,formatter,defaultDebug);
68
69 }
70
71 /**
72 * Use this if you want a class to override the global nature of a property
73 * file approach. This gives a class the option of turning debug code
74 * on or off no matter what the global value. This will not effect the
75 * global setting.
76 * @param con Connection to jdbc data source.
77 * @param stmt sql statement that will be executed.
78 * @param debug sets the debug level for this statement. DebugLevel can be OFF, ON, VERBOSE
79 * @return PreparedStatement returns a DebuggableStatement if debug = ON or VERBOSE. Returns a standard
80 * PreparedStatement if debug = OFF.
81 * @exception SQLException thrown if problem with connection.
82 */
83 public static PreparedStatement getStatement(Connection con, String stmt,
84 DebugLevel debug) throws SQLException{
85 pjaol 1.1
86 return StatementFactory.getStatement(con,stmt,defaultFormatter,debug);
87
88 }
89
90 /**
91 * this is the typical way to retrieve a statement. This method uses the static
92 * formatter and debug level.
93 * @param con Connection to jdbc data source.
94 * @param stmt sql statement that will be executed.
95 * @return PreparedStatement returns a DebuggableStatement if debug = ON or VERBOSE. Returns a standard
96 * PreparedStatement if debug = OFF.
97 * @exception SQLException thrown if problem with connection.
98 */
99 public static PreparedStatement getStatement(Connection con, String stmt) throws SQLException{
100
101 return StatementFactory.getStatement(con,stmt,defaultFormatter,defaultDebug);
102 }
103
104 /**
105 * typically set from property file so change is made in one place.
106 pjaol 1.1 * default is to false which immulates a preparedstatement.
107 * This will change debug value in all places.
108 * @param debug sets the debug level for this statement. DebugLevel can be OFF, ON, VERBOSE
109 */
110 public static void setDefaultDebug(DebugLevel debug){
111 defaultDebug = debug;
112 }
113
114 /**
115 * typically set from property file so change is made in one place.
116 * This will change formatter in all places.
117 * @param formatter sets the SqlFormatter to the database type used in this
118 * application.
119 */
120 public static void setDefaultFormatter(SqlFormatter formatter){
121 defaultFormatter = formatter;
122 }
123 }
124
|