1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public class CustomTableModel extends DefaultTableModel {
public static SQLExecuteQuery connexion;
private ResultSetMetaData md;
private int colonnes;
public Vector data;
private String[] columnNames;
// DefaultTableModel has its own internal storage; use that
public CustomTableModel(String pilote,String host,String login,String pw,String query) {
connexion = new SQLExecuteQuery(pilote,host,login,pw);
connexion.setQuery(query);
getHSQLdb();
}
public void getHSQLdb() {
try {
ResultSet rs = connexion.getResultSet();
md = rs.getMetaData();
colonnes = md.getColumnCount();
System.out.println(colonnes);
columnNames = new String[colonnes];
for (int i = 0; i <= colonnes-1; i++) {
columnNames[i] = md.getColumnLabel(i+1);
super.addColumn(columnNames[i]);
}
// Get all rows.
System.out.println("Data:");
data = new Vector();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= getColumnCount(); i++) {
newRow.addElement(rs.getObject(i));
}
data.addElement(newRow);
//System.out.println(data);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Erreur : " + ex, "Warning", JOptionPane.ERROR_MESSAGE);
}
}
public String getColumnName(int col) {
return columnNames[col];
}
public int getRowCount() {
try {
return data.size();
}
catch (Exception ex) {
return 0;
}
}
public Object getValueAt(int row, int col) {
Vector vect = (Vector)data.elementAt(row);
System.out.println(vect.elementAt(col));
return vect.elementAt(col);
}
public boolean isCellEditable(int row,int column) {
return true;
}
// let's use DefaultTableModel's own getRowCount implementation
// and getValueAt
// there is no clear method, so let's add this one
public void clear() {
// setting the number of rows to 0 also clears the model
setRowCount(0);
}
} |
Partager