00001
00002
00003
00004
00005
00006
00007
00008
00009 package dochelper;
00010
00011 import dochelper.exceptions.InvalidValueException;
00012 import dochelper.exceptions.MissingArgumentException;
00013 import dochelper.exceptions.ResourceUnavailableException;
00014 import java.sql.Connection;
00015 import java.sql.DriverManager;
00016 import java.sql.ResultSet;
00017 import java.sql.ResultSetMetaData;
00018 import java.sql.SQLException;
00019 import java.sql.Statement;
00020 import java.util.ArrayList;
00021 import java.util.HashMap;
00022 import java.util.List;
00023
00028 public class JdbcTableRecordSet extends AbstractTableRecordSet {
00029
00031 protected String USER = "user";
00032
00034 protected String PASS = "pass";
00035
00037 protected String URL = "url";
00038
00040 protected String DRIVER = "driver";
00041
00043 protected String TABLE = "tablename";
00044
00046 protected String FIELDS = "fields";
00047
00049 protected String FILTER = "whereclause";
00050
00052 private ArrayList<Record> records = new ArrayList<Record>();
00053
00054 protected void initImpl() throws SQLException, ResourceUnavailableException {
00055
00056 String url = (String)params.get(URL);
00057 String pass = (String)params.get(PASS);
00058 String user = (String)params.get(USER);
00059
00060 Connection conn;
00061 try {
00062 conn = DriverManager.getConnection(url, user, pass);
00063 }
00064 catch (Exception e) {
00065 throw new ResourceUnavailableException("Unable to connection to Database. \n" + e.getMessage());
00066 }
00067
00068
00069 fetchRecords(conn);
00070
00071 }
00072
00073 protected void checkParamsImpl() throws MissingArgumentException, InvalidValueException, ResourceUnavailableException {
00074
00075
00076 if (!params.containsKey(USER))
00077 throw new MissingArgumentException(USER);
00078
00079
00080 if (!params.containsKey(URL))
00081 throw new MissingArgumentException(URL);
00082
00083
00084 if (!params.containsKey(DRIVER))
00085 throw new MissingArgumentException(DRIVER);
00086
00087
00088 if (!params.containsKey(TABLE))
00089 throw new MissingArgumentException(TABLE);
00090
00091
00092 if (!params.containsKey(FIELDS))
00093 throw new MissingArgumentException(FIELDS);
00094
00095
00096 try {
00097 System.out.println("Trying to load class:" + (String)params.get(DRIVER));
00098 Class.forName((String)params.get(DRIVER));
00099 }
00100 catch (ClassNotFoundException cnfe) {
00101 throw new ResourceUnavailableException();
00102 }
00103
00104 }
00105
00106 protected List<Record> getRecords() throws ResourceUnavailableException, SQLException {
00107 return records;
00108 }
00109
00115 protected void fetchRecords(Connection conn) throws SQLException {
00116
00117
00118 Statement s = conn.createStatement();
00119 String query = "SELECT * FROM " + (String)params.get(TABLE);
00120
00121 String where = (String)params.get(FILTER);
00122 if (where != null)
00123 query += " WHERE " + where;
00124
00125 System.out.println("Query: " + query);
00126
00127
00128 s.executeQuery(query);
00129
00130
00131 ResultSet rs = s.getResultSet();
00132 String[] fields = ((String)params.get(FIELDS)).split(",");
00133
00134
00135 while (rs.next()) {
00136
00137 HashMap<String, Object> fieldValues = new HashMap<String, Object>();
00138 HashMap<String, Object> fullFieldValues = new HashMap<String, Object>();
00139
00140 ResultSetMetaData rsMeta = rs.getMetaData();
00141
00142
00143 for (int i = 0;i < fields.length;i++) {
00144 fieldValues.put(fields[i], rs.getObject(fields[i]));
00145 }
00146
00147
00148 for (int i = 1;i < rsMeta.getColumnCount();i++) {
00149 Object value = rs.getObject(i);
00150 if (value == null) value = new String("null");
00151 fullFieldValues.put(rsMeta.getColumnName(i), value);
00152 }
00153
00154
00155 TableRecord record = new TableRecord(fieldValues, fullFieldValues, this.getName() + "Record");
00156
00157 records.add(record);
00158 }
00159
00160
00161 rs.close();
00162 s.close();
00163
00164 }
00165 }