KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > support > AbstractSqlTypeValue


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

16
17 package org.springframework.jdbc.core.support;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 import org.springframework.jdbc.core.SqlTypeValue;
24
25 /**
26  * Abstract implementation of the SqlTypeValue interface, for convenient
27  * creation of type values that are supposed to be passed into the
28  * <code>PreparedStatement.setObject</code> method. The <code>createTypeValue</code>
29  * callback method has access to the underlying Connection, if that should
30  * be needed to create any database-specific objects.
31  *
32  * <p>A usage example from a StoredProcedure (compare this to the plain
33  * SqlTypeValue version in the superclass javadoc):
34  *
35  * <pre class="code">proc.declareParameter(new SqlParameter("myarray", Types.ARRAY, "NUMBERS"));
36  * ...
37  *
38  * Map in = new HashMap();
39  * in.put("myarray", new AbstractSqlTypeValue() {
40  * public Object createTypeValue(Connection con, int sqlType, String typeName) throws SQLException {
41  * oracle.sql.ArrayDescriptor desc = new oracle.sql.ArrayDescriptor(typeName, con);
42  * return new oracle.sql.ARRAY(desc, con, seats);
43  * }
44  * });
45  * Map out = execute(in);
46  * </pre>
47  *
48  * @author Juergen Hoeller
49  * @since 1.1
50  * @see java.sql.PreparedStatement#setObject(int, Object, int)
51  * @see org.springframework.jdbc.object.StoredProcedure
52  */

53 public abstract class AbstractSqlTypeValue implements SqlTypeValue {
54
55     public final void setTypeValue(PreparedStatement JavaDoc ps, int paramIndex, int sqlType, String JavaDoc typeName)
56             throws SQLException JavaDoc {
57
58         Object JavaDoc value = createTypeValue(ps.getConnection(), sqlType, typeName);
59         if (sqlType == TYPE_UNKNOWN) {
60             ps.setObject(paramIndex, value);
61         }
62         else {
63             ps.setObject(paramIndex, value, sqlType);
64         }
65     }
66
67     /**
68      * Create the type value to be passed into <code>PreparedStatement.setObject</code>.
69      * @param con the JDBC Connection, if needed to create any database-specific objects
70      * @param sqlType SQL type of the parameter we are setting
71      * @param typeName the type name of the parameter
72      * @return the type value
73      * @throws SQLException if a SQLException is encountered setting
74      * parameter values (that is, there's no need to catch SQLException)
75      * @see java.sql.PreparedStatement#setObject(int, Object, int)
76      */

77     protected abstract Object JavaDoc createTypeValue(Connection JavaDoc con, int sqlType, String JavaDoc typeName) throws SQLException JavaDoc;
78
79 }
80
Popular Tags