KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > ArgTypePreparedStatementSetter


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;
18
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.Types JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.springframework.dao.InvalidDataAccessApiUsageException;
26
27 /**
28  * Simple adapter for PreparedStatementSetter that applies
29  * given arrays of arguments and JDBC argument types.
30  *
31  * @author Juergen Hoeller
32  */

33 class ArgTypePreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer {
34
35     private final Object JavaDoc[] args;
36
37     private final int[] argTypes;
38
39
40     /**
41      * Create a new ArgTypePreparedStatementSetter for the given arguments.
42      * @param args
43      * @param argTypes
44      */

45     public ArgTypePreparedStatementSetter(Object JavaDoc[] args, int[] argTypes) {
46         if ((args != null && argTypes == null) || (args == null && argTypes != null) ||
47                 (args != null && args.length != argTypes.length)) {
48             throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match");
49         }
50         this.args = args;
51         this.argTypes = argTypes;
52     }
53
54
55     public void setValues(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
56         int argIndx = 1;
57         if (this.args != null) {
58             for (int i = 0; i < this.args.length; i++) {
59                 Object JavaDoc arg = this.args[i];
60                 if (arg instanceof Collection JavaDoc && this.argTypes[i] != Types.ARRAY) {
61                     Collection JavaDoc entries = (Collection JavaDoc) arg;
62                     for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
63                         Object JavaDoc entry = it.next();
64                         StatementCreatorUtils.setParameterValue(ps, argIndx++, this.argTypes[i], null, entry);
65                     }
66                 }
67                 else {
68                     StatementCreatorUtils.setParameterValue(ps, argIndx++, this.argTypes[i], null, arg);
69                 }
70             }
71         }
72     }
73
74     public void cleanupParameters() {
75         StatementCreatorUtils.cleanupParameters(this.args);
76     }
77
78 }
79
Popular Tags