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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009-2013 The Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
26 * PreparedStatementBuilder
28 * Simple workaround for the inability to create a JDBC
29 * PreparedStatement without having a current Connection
31 * See http://stackoverflow.com/a/7127189
32 * and http://blog.stackoverflow.com/2009/06/stack-overflow-creative-commons-data-dump/
35 package org.collectionspace.services.common.storage;
37 import java.sql.Connection;
38 import java.sql.PreparedStatement;
39 import java.sql.SQLException;
41 public class PreparedStatementBuilder
45 public PreparedStatementBuilder(final String sql) {
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.
54 * (The PreparedStatement will not yet exist at the time this method is overridden.)
56 * @param preparedStatement a JDBC PreparedStatement.
57 * @throws SQLException
59 protected void preparePrepared(final PreparedStatement preparedStatement)
64 * Build a PreparedStatement by obtaining it from a JDBC Connection,
65 * then applying setup directives.
67 * @param conn a JDBC connection
68 * @return a JDBC PreparedStatement, with any
69 * @throws SQLException
71 public PreparedStatement build(final Connection conn)
74 final PreparedStatement returnable = conn.prepareStatement(sql);
75 preparePrepared(returnable);