001: //==============================================================================
002: //===
003: //=== Import (adapted class from druid project http://druid.sf.net)
004: //===
005: //=== Copyright (C) by Andrea Carboni.
006: //=== This file may be distributed under the terms of the GPL license.
007: //==============================================================================
008:
009: package org.fao.gast.lib.druid;
010:
011: import java.io.ByteArrayInputStream;
012: import java.io.FileNotFoundException;
013: import java.io.IOException;
014: import java.io.StringReader;
015: import java.sql.Connection;
016: import java.sql.PreparedStatement;
017: import java.sql.SQLException;
018: import java.sql.Time;
019: import java.sql.Timestamp;
020: import java.util.List;
021: import org.dlib.tools.FullTokenizer;
022: import org.dlib.tools.TVector;
023:
024: //==============================================================================
025:
026: public class Import {
027: public static void load(Connection conn, String table,
028: String fileName) throws SQLException,
029: FileNotFoundException, IOException, Exception {
030: DdfLoader loader = new DdfLoader();
031:
032: loader.setHandler(new ImportHandler(conn, table));
033: loader.load(fileName);
034: }
035: }
036:
037: //==============================================================================
038:
039: class ImportHandler implements DdfLoader.Handler {
040: //---------------------------------------------------------------------------
041: //---
042: //--- Constructor
043: //---
044: //---------------------------------------------------------------------------
045:
046: public ImportHandler(Connection conn, String table) {
047: this .conn = conn;
048: this .table = table;
049: }
050:
051: //---------------------------------------------------------------------------
052: //---
053: //--- Handler interface
054: //---
055: //---------------------------------------------------------------------------
056:
057: public void handleFields(List<ImportField> fields) throws Exception {
058: this .fields = fields;
059:
060: TVector tvFields = new TVector();
061: TVector tvQMarks = new TVector();
062:
063: for (ImportField field : fields) {
064: tvFields.add(field.name);
065: tvQMarks.add("?");
066: }
067:
068: String query = "INSERT INTO " + table + "(" + tvFields
069: + ") VALUES (" + tvQMarks + ")";
070:
071: stmt = conn.prepareStatement(query);
072: }
073:
074: //---------------------------------------------------------------------------
075:
076: public void handleRow(List<String> values) throws Exception {
077: for (int i = 0; i < fields.size(); i++) {
078: ImportField impf = fields.get(i);
079: SqlType sqlType = impf.sqlType;
080:
081: String token = values.get(i);
082:
083: if (token.equals(""))
084: stmt.setNull(i + 1, sqlType.iId);
085: else {
086: if (sqlType.isDate())
087: stmt.setDate(i + 1, java.sql.Date.valueOf(token));
088:
089: else if (sqlType.isTime())
090: stmt.setTime(i + 1, Time.valueOf(token));
091:
092: else if (sqlType.isTimeStamp())
093: stmt.setTimestamp(i + 1, Timestamp.valueOf(token));
094:
095: else if (sqlType.isInteger())
096: stmt.setLong(i + 1, Long.parseLong(token));
097:
098: else if (sqlType.isReal())
099: stmt.setDouble(i + 1, Double.parseDouble(token));
100:
101: else if (sqlType.isBinaryType()) {
102: byte data[] = Codec.decodeBytes(token);
103: stmt
104: .setBinaryStream(i + 1,
105: new ByteArrayInputStream(data),
106: data.length);
107: }
108:
109: //--- we must divide the case of strings and longvarchar because
110: //--- some drivers don't implement the setCharacterStream method
111:
112: else if (sqlType.isString())
113: stmt.setString(i + 1, Codec.decodeString(token));
114:
115: else if (sqlType.isLongVarChar())
116: stmt.setCharacterStream(i + 1, new StringReader(
117: Codec.decodeString(token)), token.length());
118:
119: else {
120: //--- we arrive here is the sql type is unknown
121: stmt.setNull(i + 1, sqlType.iId);
122: }
123: }
124: }
125:
126: stmt.executeUpdate();
127: }
128:
129: //---------------------------------------------------------------------------
130:
131: public void cleanUp() {
132: try {
133: if (stmt != null)
134: stmt.close();
135: } catch (SQLException e) {
136: e.printStackTrace();
137: }
138: }
139:
140: //---------------------------------------------------------------------------
141: //---
142: //--- Variables
143: //---
144: //---------------------------------------------------------------------------
145:
146: private Connection conn;
147: private String table;
148:
149: private List<ImportField> fields;
150: private PreparedStatement stmt;
151: }
152:
153: //==============================================================================
|