1 7 8 package org.jdesktop.dataset.provider.sql; 9 import java.sql.PreparedStatement ; 10 import java.sql.ResultSet ; 11 import java.sql.ResultSetMetaData ; 12 import java.util.ArrayList ; 13 import java.util.HashMap ; 14 import java.util.List ; 15 import java.util.Map ; 16 import org.jdesktop.dataset.DataColumn; 17 import org.jdesktop.dataset.DataProvider; 18 import org.jdesktop.dataset.DataRow; 19 import org.jdesktop.dataset.DataTable; 20 import org.jdesktop.dataset.event.TableChangeEvent; 21 import org.jdesktop.dataset.provider.LoadTask; 22 import org.jdesktop.dataset.provider.SaveTask; 23 24 31 public class SQLDataProvider extends DataProvider { 32 private TableCommand tableCommand; 33 private JDBCDataConnection conn; 34 35 38 public SQLDataProvider() { 39 } 40 41 public SQLDataProvider(String tableName) { 42 tableCommand = new TableCommand(tableName); 43 } 44 45 public SQLDataProvider(String tableName, String whereClause) { 46 tableCommand = new TableCommand(tableName, whereClause); 47 } 48 49 public void setTableCommand(TableCommand command) { 50 this.tableCommand = command; 51 } 53 54 public TableCommand getTableCommand() { 55 return tableCommand; 56 } 57 58 public void setDataConnection(JDBCDataConnection c) { 59 this.conn = c; 60 } 61 62 public JDBCDataConnection getDataConnection() { 63 return conn; 64 } 65 66 69 protected LoadTask createLoadTask(DataTable[] tables) { 70 return new LoadTask(tables) { 71 protected void readData(DataTable[] tables) throws Exception { 72 if (conn == null) { 73 return; 75 } 76 if (tableCommand == null) { 77 return; 79 } 80 82 setMinimum(0); 84 setMaximum(tables.length); 85 for (DataTable table : tables) { 88 try { 89 PreparedStatement stmt = tableCommand.getSelectStatement(conn); 90 ResultSet rs = stmt.executeQuery(); 91 92 ResultSetMetaData md = rs.getMetaData(); 96 List <String > names = new ArrayList <String >(); 97 List <DataColumn> columns = table.getColumns(); 98 for (int i=0; i<columns.size(); i++) { 99 String name = columns.get(i).getName(); 100 for (int j=0; j<md.getColumnCount(); j++) { 101 if (name.equalsIgnoreCase(md.getColumnName(j+1))) { 102 names.add(name); 103 } 104 } 105 } 106 107 List <Map <String ,Object >> rows = new ArrayList <Map <String ,Object >>(60); 109 while (rs.next()) { 110 if (rows.size() >= 50) { 111 LoadItem item = new LoadItem<List <Map <String ,Object >>>(table, rows); 112 scheduleLoad(item); 113 rows = new ArrayList <Map <String ,Object >>(60); 114 } 115 Map <String ,Object > row = new HashMap <String ,Object >(); 117 for (String name : names) { 118 row.put(name, rs.getObject(name)); 119 } 120 rows.add(row); 121 } 122 rs.close(); 124 LoadItem item = new LoadItem<List <Map <String ,Object >>>(table, rows); 126 scheduleLoad(item); 127 } catch (Exception e) { 128 e.printStackTrace(); 129 } 130 setProgress(getProgress() + 1); 131 } 132 setProgress(getMaximum()); 133 } 134 135 138 protected void loadData(LoadItem[] items) { 139 for (LoadItem<List <Map <String ,Object >>> item : items) { 140 for (Map <String ,Object > row : item.data) { 141 DataRow r = item.table.appendRowNoEvent(); 142 for (String col : row.keySet()) { 143 r.setValue(col, row.get(col)); 144 } 145 r.setStatus(DataRow.DataRowStatus.UNCHANGED); 146 } 147 item.table.fireDataTableChanged(new TableChangeEvent(item.table)); 148 } 149 } 150 }; 151 } 152 153 156 protected SaveTask createSaveTask(DataTable[] tables) { 157 return new SaveTask(tables) { 158 protected void saveData(DataTable[] tables) throws Exception { 159 if (conn == null) { 160 return; 162 } 163 if (tableCommand == null) { 164 return; 166 } 167 169 setMinimum(0); 171 setMaximum(tables.length); 172 for (DataTable table : tables) { 173 List <DataRow> rows = table.getRows(); 175 for (DataRow row : rows) { 178 PreparedStatement stmt = null; 179 switch (row.getStatus()) { 180 case UPDATED: 181 stmt = tableCommand.getUpdateStatement(conn, row); 182 break; 183 case INSERTED: 184 stmt = tableCommand.getInsertStatement(conn, row); 185 break; 186 case DELETED: 187 stmt = tableCommand.getDeleteStatement(conn, row); 188 break; 189 default: 190 break; 192 } 193 194 if (stmt != null) { 195 conn.executeUpdate(stmt); 196 } 197 } 198 setProgress(getProgress() + 1); 199 } 200 setProgress(getMaximum()); 201 } 202 }; 203 } 204 205 } 206 | Popular Tags |