]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
756517a32750e0b003a438a99779edfb067e7e39
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright © 2009-2013 The Regents of the University of California 
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24
25 /**
26  * PreparedStatementBuilder
27  * 
28  * Simple workaround for the inability to create a JDBC
29  * PreparedStatement without having a current Connection
30  * 
31  * See http://stackoverflow.com/a/7127189
32  * and http://blog.stackoverflow.com/2009/06/stack-overflow-creative-commons-data-dump/
33  */
34
35 package org.collectionspace.services.common.storage;
36
37 import java.sql.Connection;
38 import java.sql.PreparedStatement;
39 import java.sql.SQLException;
40
41 public class PreparedStatementBuilder
42 {
43     private String sql;
44
45     public PreparedStatementBuilder(final String sql) {
46         this.sql = sql;
47     }
48
49    /**
50     * A 'virtual' method to be overridden, in which to declare setup directives
51     * to be applied to a PreparedStatement; for instance, to add values to
52     * replaceable parameters or otherwise modify the statement's behavior.
53     * 
54     * (The PreparedStatement will not yet exist at the time this method is overridden.)
55     * 
56     * @param preparedStatement a JDBC PreparedStatement.
57     * @throws SQLException 
58     */
59     protected void preparePrepared(final PreparedStatement preparedStatement) 
60             throws SQLException {
61     }
62
63     /**
64      * Build a PreparedStatement by obtaining it from a JDBC Connection,
65      * then applying setup directives.
66      * 
67      * @param conn a JDBC connection
68      * @return a JDBC PreparedStatement, with any 
69      * @throws SQLException 
70      */
71     public PreparedStatement build(final Connection conn)
72             throws SQLException
73     {
74         final PreparedStatement returnable = conn.prepareStatement(sql);
75         preparePrepared(returnable);
76         return returnable;
77     }
78 }