01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.data.jdbc.attributeio;
17:
18: import java.io.IOException;
19: import java.io.Serializable;
20: import java.sql.PreparedStatement;
21: import java.sql.ResultSet;
22: import java.sql.SQLException;
23: import java.sql.Types;
24:
25: import org.geotools.data.DataSourceException;
26:
27: /**
28: * A basic attribute IO class, that will parse and write attributes
29: * as objects (using getObject, updateObject). This works usually with
30: * all primitives wrapped by an objects, and of course with every {@link Serializable}
31: * object.
32: *
33: * @author wolf
34: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/jdbc/src/main/java/org/geotools/data/jdbc/attributeio/BasicAttributeIO.java $
35: */
36: public class BasicAttributeIO implements AttributeIO {
37: /**
38: * @see org.geotools.data.jdbc.attributeio.AttributeIO#read(java.sql.ResultSet,
39: * int)
40: */
41: public Object read(ResultSet rs, int position) throws IOException {
42: try {
43: return rs.getObject(position);
44: } catch (SQLException e) {
45: throw new DataSourceException("Sql problem.", e);
46: }
47: }
48:
49: /**
50: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.ResultSet,
51: * int, java.lang.Object)
52: */
53: public void write(ResultSet rs, int position, Object value)
54: throws IOException {
55: try {
56: if (value == null) {
57: rs.updateNull(position);
58: } else {
59: rs.updateObject(position, value);
60: }
61: } catch (Exception e) {
62: throw new DataSourceException("Sql problem.", e);
63: }
64: }
65:
66: /**
67: * @see org.geotools.data.jdbc.attributeio.AttributeIO#write(java.sql.PreparedStatement, int, java.lang.Object)
68: */
69: public void write(PreparedStatement ps, int position, Object value)
70: throws IOException {
71: try {
72: if (value == null) {
73: ps.setNull(position, Types.OTHER);
74: } else {
75: ps.setObject(position, value);
76: }
77: } catch (Exception e) {
78: throw new DataSourceException("Sql problem.", e);
79: }
80:
81: }
82:
83: }
|