View Javadoc
1 package net.plugin.sql.gui.model.table; 2 3 import net.plugin.sql.beans.DataSource; 4 import net.plugin.sql.util.JDBCConnectionManager; 5 import net.plugin.sql.util.DataSourceException; 6 7 import javax.swing.table.AbstractTableModel; 8 import javax.swing.*; 9 import java.sql.*; 10 import javax.sql.RowSet; 11 import javax.sql.RowSetEvent; 12 import javax.sql.RowSetListener; 13 14 import sun.jdbc.rowset.CachedRowSet; 15 16 public class CachedRowSetTableInfoModel extends AbstractTableModel implements RowSetListener { 17 private RowSet rowSet = null; 18 19 private static final String[] HEADERS = {"Name", "Type", "Length", "Nullable"}; 20 21 public CachedRowSetTableInfoModel(RowSet set) { 22 super(); 23 rowSet = set; 24 rowSet.addRowSetListener(this); 25 } 26 27 public void cursorMoved(RowSetEvent event) { 28 } 29 30 public Class getColumnClass(int column) { 31 String cname = "java.lang.String"; 32 switch (column) { 33 case 2: 34 { 35 cname = "java.lang.Integer"; 36 break; 37 } 38 case 3: 39 { 40 cname = "java.lang.Boolean"; 41 break; 42 } 43 } 44 try { 45 return Class.forName(cname); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 return super.getColumnClass(column); 49 } 50 } 51 52 public int getColumnCount() { 53 return HEADERS.length; 54 } 55 56 public String getColumnName(int col) { 57 return HEADERS[col]; 58 } 59 60 public int getRowCount() { 61 try { 62 ResultSetMetaData meta = rowSet.getMetaData(); 63 if (meta == null) { 64 return 0; 65 } 66 return meta.getColumnCount(); 67 } catch (SQLException e) { 68 return 0; 69 } 70 } 71 72 public Object getValueAt(int row, int col) { 73 try { 74 ResultSetMetaData meta = rowSet.getMetaData(); 75 switch (col) { 76 case 0: 77 { 78 return meta.getColumnName(row+1); 79 } 80 case 1: 81 { 82 return meta.getColumnTypeName(row+1); 83 } 84 case 2: 85 { 86 return new Integer(meta.getColumnDisplaySize(row+1)); 87 } 88 case 3: 89 { 90 if (meta.isNullable(row+1) == ResultSetMetaData.columnNullable) 91 return new Boolean(true); 92 return new Boolean(false); 93 } 94 95 } 96 } catch (SQLException e) { 97 return null; 98 } 99 return null; 100 } 101 102 public void rowChanged(RowSetEvent event) { 103 try { 104 int row = rowSet.getRow(); 105 106 if (rowSet.rowDeleted()) { 107 fireTableRowsDeleted(row, row); 108 } else if (rowSet.rowInserted()) { 109 fireTableRowsInserted(row, row); 110 } else if (rowSet.rowUpdated()) { 111 fireTableRowsUpdated(row, row); 112 } 113 } catch (SQLException e) { 114 } 115 } 116 117 public void rowSetChanged(RowSetEvent event) { 118 fireTableStructureChanged(); 119 } 120 121 public void setValueAt(Object value, int row, int column) { 122 try { 123 if (!rowSet.absolute(row + 1)) { 124 return; 125 } 126 rowSet.updateObject(column + 1, value); 127 } catch (SQLException e) { 128 } 129 } 130 131 public static void main(String[] args) { 132 DataSource ds = new DataSource(); 133 ds.setName("Test"); 134 ds.setDriver("oracle.jdbc.driver.OracleDriver"); 135 ds.setUrl("jdbc:oracle:thin:@hades.genscape.com:1581:test"); 136 ds.setUser("genscape"); 137 ds.setPassword("gentest"); 138 JDBCConnectionManager manager = new JDBCConnectionManager(ds); 139 Connection con = null; 140 Statement stmt = null; 141 ResultSet rs = null; 142 CachedRowSet crs = null; 143 try { 144 con = manager.getConnection(); 145 stmt = con.createStatement(); 146 rs = stmt.executeQuery("select * from plant"); 147 stmt.setMaxRows(25); 148 crs = new CachedRowSet(); 149 crs.populate(rs); 150 } catch (DataSourceException e) { 151 e.printStackTrace(); 152 } catch (SQLException e) { 153 e.printStackTrace(); 154 } finally { 155 try { 156 rs.close(); 157 stmt.close(); 158 con.close(); 159 } catch (SQLException e) { 160 e.printStackTrace(); 161 } 162 } 163 JFrame frame = new JFrame("Test"); 164 // CachedRowSetTableInfoModel model = new CachedRowSetTableInfoModel(crs); 165 CachedRowSetTableInfoModel infoModel = new CachedRowSetTableInfoModel(crs); 166 JTable table = new JTable(infoModel); 167 JScrollPane jScrollPane = new JScrollPane(table); 168 frame.getContentPane().add(jScrollPane); 169 frame.pack(); 170 frame.show(); 171 } 172 173 }

This page was automatically generated by Maven