001: package org.julp;
002:
003: import java.sql.*;
004: import java.util.*;
005:
006: public class Parameters implements ParameterMetaData,
007: java.io.Serializable {
008:
009: // params - the first parameter is 1, the second is 2, ...
010: protected Object[] params;
011: protected int[] parameterMode;
012: protected int[] parameterType;
013: protected String[] parameterTypeName;
014: protected int[] precision;
015: protected int[] scale;
016: protected int[] nullable;
017: protected boolean[] signed;
018: protected String[] parameterName;
019: protected int parameterCount;
020:
021: public Parameters() {
022: }
023:
024: public boolean isWrapperFor(Class iface) {
025: return false;
026: }
027:
028: public Class unwrap(Class iface) {
029: return null;
030: }
031:
032: public Parameters(int parameterCount) {
033: setParams(new Object[parameterCount]);
034: }
035:
036: protected void init() {
037: this .parameterCount = this .params.length;
038: this .parameterMode = new int[parameterCount];
039: this .parameterType = new int[parameterCount];
040: this .parameterTypeName = new String[parameterCount];
041: this .precision = new int[parameterCount];
042: this .scale = new int[parameterCount];
043: this .nullable = new int[parameterCount];
044: this .signed = new boolean[parameterCount];
045: this .parameterName = new String[parameterCount];
046: }
047:
048: public Parameters(Object[] params) {
049: setParams(params);
050: }
051:
052: public String getParameterClassName(int index) throws SQLException {
053: return params[index - 1].getClass().getName();
054: }
055:
056: public int getParameterCount() throws SQLException {
057: return parameterCount;
058: }
059:
060: public int getParameterMode(int index) throws SQLException {
061: return parameterMode[index - 1];
062: }
063:
064: public void setParameterMode(int index, int parameterMode)
065: throws SQLException {
066: this .parameterMode[index - 1] = parameterMode;
067: }
068:
069: public int getParameterType(int index) throws SQLException {
070: return parameterType[index - 1];
071: }
072:
073: public void setParameterType(int index, int parameterType)
074: throws SQLException {
075: this .parameterType[index - 1] = parameterType;
076: }
077:
078: public String getParameterTypeName(int index) throws SQLException {
079: return parameterTypeName[index - 1];
080: }
081:
082: public void setParameterTypeName(int index, String parameterTypeName)
083: throws SQLException {
084: this .parameterTypeName[index - 1] = parameterTypeName;
085: }
086:
087: public int getPrecision(int index) throws SQLException {
088: return precision[index - 1];
089: }
090:
091: public void setPrecision(int index, int precision)
092: throws SQLException {
093: this .precision[index - 1] = precision;
094: }
095:
096: public int getScale(int index) throws SQLException {
097: return scale[index - 1];
098: }
099:
100: public void setScale(int index, int scale) throws SQLException {
101: this .scale[index - 1] = scale;
102: }
103:
104: public int isNullable(int index) throws SQLException {
105: return nullable[index - 1];
106: }
107:
108: public void setNullable(int index, int nullable)
109: throws SQLException {
110: this .nullable[index - 1] = nullable;
111: }
112:
113: public boolean isSigned(int index) throws SQLException {
114: return signed[index - 1];
115: }
116:
117: public void setSigned(int index, boolean signed)
118: throws SQLException {
119: this .signed[index - 1] = signed;
120: }
121:
122: public Object[] getParams() {
123: return params;
124: }
125:
126: public void setParams(Object[] params) {
127: this .params = params;
128: init();
129: }
130:
131: public Object getParameter(int index) {
132: return params[index - 1];
133: }
134:
135: public Object getParameter(String parameterName)
136: throws SQLException {
137: return params[getParameterIndex(parameterName) - 1];
138: }
139:
140: public void setParameter(int index, Object value) {
141: this .params[index - 1] = value;
142: }
143:
144: public String getParameterName(int index) throws SQLException {
145: return parameterName[index - 1];
146: }
147:
148: public void setParameterName(int index, String parameterName)
149: throws SQLException {
150: if (parameterName == null || parameterName.trim().length() == 0) {
151: throw new SQLException("Parameter name is missing");
152: }
153: parameterName = parameterName.trim();
154: for (int i = 0; i < parameterCount; i++) {
155: String name = this .parameterName[i];
156: if (name != null && name.equalsIgnoreCase(parameterName)) {
157: throw new SQLException("Duplicate parameter name: "
158: + parameterName);
159: }
160: }
161: this .parameterName[index - 1] = parameterName;
162: }
163:
164: public int getParameterIndex(String parameterName)
165: throws SQLException {
166: if (parameterName == null || parameterName.trim().length() == 0) {
167: return -1;
168: }
169: for (int i = 0; i < parameterCount; i++) {
170: String name = this .parameterName[i];
171: if (name != null && name.equalsIgnoreCase(parameterName)) {
172: return i + 1;
173: }
174: }
175: return -1;
176: }
177:
178: public String toString() {
179: StringBuffer sb = new StringBuffer();
180: for (int i = 0; i < parameterCount; i++) {
181: sb.append("\nparam").append(i + 1).append(": ");
182: sb.append("parameterName=").append(parameterName[i]);
183: sb.append("&value=").append(params[i]);
184: sb.append("¶meterMode=").append(parameterMode[i]);
185: sb.append("¶meterType=").append(parameterType[i]);
186: sb.append("¶meterTypeName=").append(
187: parameterTypeName[i]);
188: sb.append("&precision=").append(precision[i]);
189: sb.append("&scale=").append(scale[i]);
190: sb.append("&nullable=").append(nullable[i]);
191: sb.append("&signed=").append(signed[i]);
192: }
193: return sb.toString();
194: }
195: }
|