KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * Object to represent a SQL parameter definition.
24  *
25  * <p>Parameters may be anonymous, in which case "name" is <code>null</code>.
26  * However, all parameters must define a SQL type according to {@link java.sql.Types}.
27  *
28  * @author Rod Johnson
29  * @author Thomas Risberg
30  * @see java.sql.Types
31  */

32 public class SqlParameter {
33
34     private final String JavaDoc name;
35     
36     /** SQL type constant from <code>java.sql.Types</code> */
37     private final int sqlType;
38
39     /** Used for types that are user-named like: STRUCT, DISTINCT, JAVA_OBJECT, named array types */
40     private final String JavaDoc typeName;
41
42
43     /**
44      * Create a new anonymous SqlParameter, supplying SQL type.
45      * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
46      */

47     public SqlParameter(int sqlType) {
48         this(null, sqlType, null);
49     }
50
51     /**
52      * Create a new anonymous SqlParameter, supplying SQL type.
53      * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
54      * @param typeName the type name of the parameter (optional)
55      */

56     public SqlParameter(int sqlType, String JavaDoc typeName) {
57         this(null, sqlType, typeName);
58     }
59
60     /**
61      * Create a new SqlParameter, supplying name and SQL type.
62      * @param name name of the parameter, as used in input and output maps
63      * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
64      */

65     public SqlParameter(String JavaDoc name, int sqlType) {
66         this(name, sqlType, (String JavaDoc) null);
67     }
68     
69     /**
70      * Create a new SqlParameter, supplying name and SQL type.
71      * @param name name of the parameter, as used in input and output maps
72      * @param sqlType SQL type of the parameter according to <code>java.sql.Types</code>
73      * @param typeName the type name of the parameter (optional)
74      */

75     public SqlParameter(String JavaDoc name, int sqlType, String JavaDoc typeName) {
76         this.name = name;
77         this.sqlType = sqlType;
78         this.typeName = typeName;
79     }
80
81
82     /**
83      * Return the name of the parameter.
84      */

85     public String JavaDoc getName() {
86         return this.name;
87     }
88
89     /**
90      * Return the SQL type of the parameter.
91      */

92     public int getSqlType() {
93         return this.sqlType;
94     }
95
96     /**
97      * Return the type name of the parameter, if any.
98      */

99     public String JavaDoc getTypeName() {
100         return this.typeName;
101     }
102
103
104     /**
105      * Convert a list of JDBC types, as defined in <code>java.sql.Types</code>,
106      * to a List of SqlParameter objects as used in this package.
107      */

108     public static List JavaDoc sqlTypesToAnonymousParameterList(int[] types) {
109         List JavaDoc result = new LinkedList JavaDoc();
110         if (types != null) {
111             for (int i = 0; i < types.length; i++) {
112                 result.add(new SqlParameter(types[i]));
113             }
114         }
115         return result;
116     }
117
118 }
119
Popular Tags