1 36 37 40 41 47 48 import java.util.Vector ; 49 import java.sql.*; 50 import javax.swing.table.AbstractTableModel ; 51 import javax.swing.event.TableModelEvent ; 52 53 public class JDBCAdapter extends AbstractTableModel { 54 Connection connection; 55 Statement statement; 56 ResultSet resultSet; 57 String [] columnNames = {}; 58 Vector rows = new Vector (); 59 ResultSetMetaData metaData; 60 61 public JDBCAdapter(String url, String driverName, 62 String user, String passwd) { 63 try { 64 Class.forName(driverName); 65 System.out.println("Opening db connection"); 66 67 connection = DriverManager.getConnection(url, user, passwd); 68 statement = connection.createStatement(); 69 } 70 catch (ClassNotFoundException ex) { 71 System.err.println("Cannot find the database driver classes."); 72 System.err.println(ex); 73 } 74 catch (SQLException ex) { 75 System.err.println("Cannot connect to this database."); 76 System.err.println(ex); 77 } 78 } 79 80 public void executeQuery(String query) { 81 if (connection == null || statement == null) { 82 System.err.println("There is no database to execute the query."); 83 return; 84 } 85 try { 86 resultSet = statement.executeQuery(query); 87 metaData = resultSet.getMetaData(); 88 89 int numberOfColumns = metaData.getColumnCount(); 90 columnNames = new String [numberOfColumns]; 91 for(int column = 0; column < numberOfColumns; column++) { 94 columnNames[column] = metaData.getColumnLabel(column+1); 95 } 96 97 rows = new Vector (); 99 while (resultSet.next()) { 100 Vector newRow = new Vector (); 101 for (int i = 1; i <= getColumnCount(); i++) { 102 newRow.addElement(resultSet.getObject(i)); 103 } 104 rows.addElement(newRow); 105 } 106 fireTableChanged(null); } 109 catch (SQLException ex) { 110 System.err.println(ex); 111 } 112 } 113 114 public void close() throws SQLException { 115 System.out.println("Closing db connection"); 116 resultSet.close(); 117 statement.close(); 118 connection.close(); 119 } 120 121 protected void finalize() throws Throwable { 122 close(); 123 super.finalize(); 124 } 125 126 132 134 public String getColumnName(int column) { 135 if (columnNames[column] != null) { 136 return columnNames[column]; 137 } else { 138 return ""; 139 } 140 } 141 142 public Class getColumnClass(int column) { 143 int type; 144 try { 145 type = metaData.getColumnType(column+1); 146 } 147 catch (SQLException e) { 148 return super.getColumnClass(column); 149 } 150 151 switch(type) { 152 case Types.CHAR: 153 case Types.VARCHAR: 154 case Types.LONGVARCHAR: 155 return String .class; 156 157 case Types.BIT: 158 return Boolean .class; 159 160 case Types.TINYINT: 161 case Types.SMALLINT: 162 case Types.INTEGER: 163 return Integer .class; 164 165 case Types.BIGINT: 166 return Long .class; 167 168 case Types.FLOAT: 169 case Types.DOUBLE: 170 return Double .class; 171 172 case Types.DATE: 173 return java.sql.Date .class; 174 175 default: 176 return Object .class; 177 } 178 } 179 180 public boolean isCellEditable(int row, int column) { 181 try { 182 return metaData.isWritable(column+1); 183 } 184 catch (SQLException e) { 185 return false; 186 } 187 } 188 189 public int getColumnCount() { 190 return columnNames.length; 191 } 192 193 195 public int getRowCount() { 196 return rows.size(); 197 } 198 199 public Object getValueAt(int aRow, int aColumn) { 200 Vector row = (Vector )rows.elementAt(aRow); 201 return row.elementAt(aColumn); 202 } 203 204 public String dbRepresentation(int column, Object value) { 205 int type; 206 207 if (value == null) { 208 return "null"; 209 } 210 211 try { 212 type = metaData.getColumnType(column+1); 213 } 214 catch (SQLException e) { 215 return value.toString(); 216 } 217 218 switch(type) { 219 case Types.INTEGER: 220 case Types.DOUBLE: 221 case Types.FLOAT: 222 return value.toString(); 223 case Types.BIT: 224 return ((Boolean )value).booleanValue() ? "1" : "0"; 225 case Types.DATE: 226 return value.toString(); default: 228 return "\""+value.toString()+"\""; 229 } 230 231 } 232 233 public void setValueAt(Object value, int row, int column) { 234 try { 235 String tableName = metaData.getTableName(column+1); 236 if (tableName == null) { 238 System.out.println("Table name returned null."); 239 } 240 String columnName = getColumnName(column); 241 String query = 242 "update "+tableName+ 243 " set "+columnName+" = "+dbRepresentation(column, value)+ 244 " where "; 245 for(int col = 0; col<getColumnCount(); col++) { 249 String colName = getColumnName(col); 250 if (colName.equals("")) { 251 continue; 252 } 253 if (col != 0) { 254 query = query + " and "; 255 } 256 query = query + colName +" = "+ 257 dbRepresentation(col, getValueAt(row, col)); 258 } 259 System.out.println(query); 260 System.out.println("Not sending update to database"); 261 } 263 catch (SQLException e) { 264 System.err.println("Update failed"); 266 } 267 Vector dataRow = (Vector )rows.elementAt(row); 268 dataRow.setElementAt(value, column); 269 270 } 271 } 272 | Popular Tags |