KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > contrib > jdbc > SimpleStatement


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.contrib.jdbc;
16
17 import java.sql.Connection JavaDoc;
18 import java.sql.ResultSet JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.sql.Statement JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * A wrapper around {@link Statement}.
27  *
28  * @author Howard Lewis Ship
29  *
30  **/

31
32 public class SimpleStatement implements IStatement
33 {
34     private static final Log LOG = LogFactory.getLog(SimpleStatement.class);
35
36     private String JavaDoc _sql;
37     private Statement JavaDoc _statement;
38
39     public SimpleStatement(String JavaDoc SQL, Connection JavaDoc connection) throws SQLException JavaDoc
40     {
41         _sql = SQL;
42         _statement = connection.createStatement();
43     }
44
45     public SimpleStatement(String JavaDoc SQL, Connection JavaDoc connection, int resultSetType, int resultSetConcurrency)
46         throws SQLException JavaDoc
47     {
48         _sql = SQL;
49         _statement = connection.createStatement(resultSetType, resultSetConcurrency);
50     }
51
52     /**
53      * Returns the SQL associated with this statement.
54      *
55      **/

56
57     public String JavaDoc getSQL()
58     {
59         return _sql;
60     }
61
62     /**
63      * Returns the underlying {@link Statement}.
64      *
65      **/

66
67     public Statement JavaDoc getStatement()
68     {
69         return _statement;
70     }
71
72     /**
73      * Closes the underlying statement, and nulls the reference to it.
74      *
75      **/

76
77     public void close() throws SQLException JavaDoc
78     {
79         _statement.close();
80
81         _statement = null;
82         _sql = null;
83     }
84
85     /**
86      * Executes the statement as a query, returning a {@link ResultSet}.
87      *
88      **/

89
90     public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc
91     {
92         if (LOG.isDebugEnabled())
93             LOG.debug("Executing query: " + this);
94
95         return _statement.executeQuery(_sql);
96     }
97
98     /**
99      * Executes the statement as an update, returning the number of rows
100      * affected.
101      *
102      **/

103
104     public int executeUpdate() throws SQLException JavaDoc
105     {
106         if (LOG.isDebugEnabled())
107             LOG.debug("Executing update: " + this);
108
109         return _statement.executeUpdate(_sql);
110     }
111
112     public String JavaDoc toString()
113     {
114         StringBuffer JavaDoc buffer;
115
116         buffer = new StringBuffer JavaDoc("SimpleStatement@");
117         buffer.append(Integer.toHexString(hashCode()));
118
119         buffer.append("[SQL=<\n");
120         buffer.append(_sql);
121         buffer.append("\n>]");
122
123         return buffer.toString();
124     }
125
126 }
Popular Tags