1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.contrib.jdbc; |
16 | |
|
17 | |
import java.sql.Connection; |
18 | |
import java.sql.PreparedStatement; |
19 | |
import java.sql.ResultSet; |
20 | |
import java.sql.SQLException; |
21 | |
import java.sql.Statement; |
22 | |
import java.util.List; |
23 | |
|
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class ParameterizedStatement implements IStatement |
35 | |
{ |
36 | 0 | private static final Log LOG = LogFactory.getLog(ParameterizedStatement.class); |
37 | |
|
38 | |
private String _sql; |
39 | |
private PreparedStatement _statement; |
40 | |
private IParameter[] _parameters; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public ParameterizedStatement(String SQL, Connection connection, List parameters) throws SQLException |
52 | 0 | { |
53 | 0 | _sql = SQL; |
54 | |
|
55 | 0 | _statement = connection.prepareStatement(SQL); |
56 | |
|
57 | 0 | _parameters = (IParameter[]) parameters.toArray(new IParameter[parameters.size()]); |
58 | |
|
59 | 0 | for (int i = 0; i < _parameters.length; i++) |
60 | |
{ |
61 | |
|
62 | |
|
63 | 0 | _parameters[i].set(_statement, i + 1); |
64 | |
} |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public String getSQL() |
73 | |
{ |
74 | 0 | return _sql; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public Statement getStatement() |
83 | |
{ |
84 | 0 | return _statement; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public void close() throws SQLException |
93 | |
{ |
94 | 0 | _statement.close(); |
95 | |
|
96 | 0 | _statement = null; |
97 | 0 | _sql = null; |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public ResultSet executeQuery() throws SQLException |
106 | |
{ |
107 | 0 | if (LOG.isDebugEnabled()) |
108 | 0 | LOG.debug("Executing query: " + this); |
109 | |
|
110 | 0 | return _statement.executeQuery(); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public int executeUpdate() throws SQLException |
120 | |
{ |
121 | 0 | if (LOG.isDebugEnabled()) |
122 | 0 | LOG.debug("Executing update: " + this); |
123 | |
|
124 | 0 | return _statement.executeUpdate(); |
125 | |
} |
126 | |
|
127 | |
public String toString() |
128 | |
{ |
129 | 0 | StringBuffer buffer = new StringBuffer("ParameterizedStatement@"); |
130 | 0 | buffer.append(Integer.toHexString(hashCode())); |
131 | 0 | buffer.append("[SQL=\n<"); |
132 | 0 | buffer.append(_sql); |
133 | 0 | buffer.append("\n>"); |
134 | |
|
135 | 0 | for (int i = 0; i < _parameters.length; i++) |
136 | |
{ |
137 | 0 | IParameter parameter = _parameters[i]; |
138 | |
|
139 | 0 | buffer.append(" ?"); |
140 | 0 | buffer.append(i + 1); |
141 | 0 | buffer.append('='); |
142 | |
|
143 | 0 | buffer.append(parameter); |
144 | |
} |
145 | |
|
146 | 0 | buffer.append(']'); |
147 | |
|
148 | 0 | return buffer.toString(); |
149 | |
} |
150 | |
|
151 | |
} |