001: package org.julp;
002:
003: import java.io.Serializable;
004: import java.sql.ResultSet;
005: import java.sql.SQLException;
006:
007: public class DefaultConverter implements Serializable, Converter {
008:
009: protected transient ResultSet resultSet;
010:
011: public DefaultConverter() {
012: }
013:
014: public DefaultConverter(ResultSet resultSet) {
015: setResultSet(resultSet);
016: }
017:
018: public Object[] convert(String fieldClassName, String columnName)
019: throws SQLException {
020: Object value = null;
021: Object[] convertedValue = null;
022: // So far there is support only for listed DataTypes. Feel free to add more...
023: if (fieldClassName == null) { //???
024: convertedValue = new Object[1];
025: value = resultSet.getObject(columnName);
026: convertedValue[0] = value;
027: } else if (fieldClassName.equals("java.lang.String")) {
028: convertedValue = new String[1];
029: value = resultSet.getString(columnName);
030: if (resultSet.wasNull()) {
031: convertedValue[0] = (String) null;
032: } else {
033: convertedValue[0] = value;
034: }
035: } else if (fieldClassName.equals("java.lang.Integer")) {
036: convertedValue = new Integer[1];
037: value = new Integer(resultSet.getInt(columnName));
038: if (resultSet.wasNull()) {
039: convertedValue[0] = (Integer) null;
040: } else {
041: convertedValue[0] = value;
042: }
043: } else if (fieldClassName.equals("java.lang.Long")) {
044: convertedValue = new Long[1];
045: value = new Long(resultSet.getLong(columnName));
046: if (resultSet.wasNull()) {
047: convertedValue[0] = (Long) null;
048: } else {
049: convertedValue[0] = value;
050: }
051: } else if (fieldClassName.equals("java.lang.Double")) {
052: convertedValue = new Double[1];
053: value = new Double(resultSet.getDouble(columnName));
054: if (resultSet.wasNull()) {
055: convertedValue[0] = (Double) null;
056: } else {
057: convertedValue[0] = value;
058: }
059: } else if (fieldClassName.equals("java.lang.Float")) {
060: convertedValue = new Float[1];
061: value = new Float(resultSet.getFloat(columnName));
062: if (resultSet.wasNull()) {
063: convertedValue[0] = (Float) null;
064: } else {
065: convertedValue[0] = value;
066: }
067: } else if (fieldClassName.equals("java.lang.Short")) {
068: convertedValue = new Short[1];
069: value = new Short(resultSet.getShort(columnName));
070: if (resultSet.wasNull()) {
071: convertedValue[0] = (Short) null;
072: } else {
073: convertedValue[0] = value;
074: }
075: } else if (fieldClassName.equals("java.sql.Timestamp")) {
076: convertedValue = new java.sql.Timestamp[1];
077: value = (java.sql.Timestamp) resultSet
078: .getTimestamp(columnName);
079: if (resultSet.wasNull()) {
080: convertedValue[0] = (java.sql.Timestamp) null;
081: } else {
082: convertedValue[0] = value;
083: }
084: } else if (fieldClassName.equals("java.sql.Time")) {
085: convertedValue = new java.sql.Time[1];
086: value = (java.sql.Time) resultSet.getTime(columnName);
087: if (resultSet.wasNull()) {
088: convertedValue[0] = (java.sql.Time) null;
089: } else {
090: convertedValue[0] = value;
091: }
092: } else if (fieldClassName.equals("java.sql.Date")) {
093: convertedValue = new java.sql.Date[1];
094: value = (java.sql.Date) resultSet.getDate(columnName);
095: if (resultSet.wasNull()) {
096: convertedValue[0] = (java.sql.Date) null;
097: } else {
098: convertedValue[0] = value;
099: }
100: } else if (fieldClassName.equals("java.util.Date")) {
101: convertedValue = new java.util.Date[1];
102: java.sql.Date d = resultSet.getDate(columnName);
103: if (resultSet.wasNull()) {
104: convertedValue[0] = (java.util.Date) null;
105: } else {
106: value = new java.util.Date(d.getTime());
107: convertedValue[0] = value;
108: }
109: } else if (fieldClassName.equals("java.math.BigDecimal")) {
110: convertedValue = new java.math.BigDecimal[1];
111: value = resultSet.getBigDecimal(columnName);
112: if (resultSet.wasNull()) {
113: convertedValue[0] = (java.math.BigDecimal) null;
114: } else {
115: convertedValue[0] = value;
116: }
117: } else if (fieldClassName.equals("java.math.BigInteger")) {
118: convertedValue = new java.math.BigInteger[1];
119: long longValue = 0;
120: longValue = resultSet.getLong(columnName);
121: if (resultSet.wasNull()) {
122: convertedValue[0] = (java.math.BigInteger) null;
123: } else {
124: value = java.math.BigInteger.valueOf(longValue);
125: convertedValue[0] = value;
126: }
127: } else if (fieldClassName.equals("boolean")) {
128: convertedValue = new Boolean[1];
129: boolean booleanValue = false;
130: booleanValue = resultSet.getBoolean(columnName);
131: if (resultSet.wasNull()) {
132: convertedValue[0] = new Boolean(false);
133: } else {
134: value = new Boolean(booleanValue);
135: convertedValue[0] = value;
136: }
137: } else if (fieldClassName.equals("byte")) {
138: convertedValue = new Byte[1];
139: byte byteValue = 0;
140: byteValue = resultSet.getByte(columnName);
141: if (resultSet.wasNull()) {
142: byte emptyByte = 0;
143: convertedValue[0] = new Byte(emptyByte);
144: } else {
145: value = new Byte(byteValue);
146: convertedValue[0] = value;
147: }
148: } else if (fieldClassName.equals("char")) {
149: convertedValue = new Character[1];
150: String stringValue = null;
151: stringValue = resultSet.getString(columnName);
152: if (resultSet.wasNull()) {
153: char emptyChar = '\u0000';
154: convertedValue[0] = new Character(emptyChar);
155: } else {
156: value = new Character(stringValue.charAt(0));
157: convertedValue[0] = value;
158: }
159: } else if (fieldClassName.equals("double")) {
160: convertedValue = new Double[1];
161: double doubleValue = 0.0;
162: doubleValue = resultSet.getDouble(columnName);
163: if (resultSet.wasNull()) {
164: convertedValue[0] = new Double(0.0);
165: } else {
166: value = new Double(doubleValue);
167: convertedValue[0] = value;
168: }
169: } else if (fieldClassName.equals("float")) {
170: convertedValue = new Float[1];
171: float floatValue = 0;
172: floatValue = resultSet.getFloat(columnName);
173: if (resultSet.wasNull()) {
174: convertedValue[0] = new Float(0.0);
175: } else {
176: value = new Float(floatValue);
177: convertedValue[0] = value;
178: }
179: } else if (fieldClassName.equals("int")) {
180: convertedValue = new Integer[1];
181: int intValue = 0;
182: intValue = resultSet.getInt(columnName);
183: if (resultSet.wasNull()) {
184: convertedValue[0] = new Integer(0);
185: } else {
186: value = new Integer(intValue);
187: convertedValue[0] = value;
188: }
189: } else if (fieldClassName.equals("long")) {
190: convertedValue = new Long[1];
191: long longValue = 0;
192: longValue = resultSet.getLong(columnName);
193: if (resultSet.wasNull()) {
194: convertedValue[0] = new Long(0);
195: } else {
196: value = new Long(longValue);
197: convertedValue[0] = value;
198: }
199: } else if (fieldClassName.equals("short")) {
200: convertedValue = new Short[1];
201: short shortValue = 0;
202: shortValue = resultSet.getShort(columnName);
203: if (resultSet.wasNull()) {
204: short s = 0;
205: convertedValue[0] = new Short(s);
206: } else {
207: value = new Short(shortValue);
208: convertedValue[0] = value;
209: }
210: } else if (fieldClassName.equals("java.lang.Boolean")) {
211: convertedValue = new Boolean[1];
212: boolean booleanValue = false;
213: booleanValue = resultSet.getBoolean(columnName);
214: if (resultSet.wasNull()) {
215: convertedValue[0] = new Boolean(false);
216: } else {
217: value = new Boolean(booleanValue);
218: convertedValue[0] = value;
219: }
220: } else if (fieldClassName.equals("java.lang.Character")) {
221: convertedValue = new Character[1];
222: String stringValue = null;
223: stringValue = resultSet.getString(columnName);
224: if (resultSet.wasNull()) {
225: char emptyChar = '\u0000';
226: convertedValue[0] = new Character(emptyChar);
227: } else {
228: value = new Character(stringValue.charAt(0));
229: convertedValue[0] = value;
230: }
231: } else {
232: convertedValue = new Object[1];
233: value = resultSet.getObject(columnName);
234: convertedValue[0] = value;
235: }
236: return convertedValue;
237: }
238:
239: public ResultSet getResultSet() {
240: return resultSet;
241: }
242:
243: public void setResultSet(ResultSet resultSet) {
244: this.resultSet = resultSet;
245: }
246: }
|