Witajcie!
Mam pytanie jak mozna edytowac Jtable zeby wyniki od razu ladowały w bazie danych.
oto kod:
public class OFrame extends JFrame {
private JTable table;
private DBmodel tablemodel;
private JPanel myP;
public OFrame(Connection conn) throws SQLException {
this.setTitle("Lista Obecności");
myP = new JPanel();
myP.setLayout(new BorderLayout());
Statement stat1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs1 = stat1.executeQuery("Select Imie, Nazwisko, godz1,godz2,godz3,godz4,godz5,godz6,godz7,godz8"
+ " From uczniowie INNER JOIN obecnosc ON uczniowie.pesel=obecnosc.pesel;");
tablemodel = new DBmodel(rs1);
table = new JTable(tablemodel);
JScrollPane scrollPane = new JScrollPane(table);
myP.add(scrollPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 700);
add(myP);
//myP.add(new JButton("Drukuj"));
setVisible(true);
}
class DBmodel extends AbstractTableModel {
private ResultSet rs;
private ResultSetMetaData rsmd;
public DBmodel(ResultSet aResultSet) {
rs = aResultSet;
try {
rsmd = rs.getMetaData();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public String getColumnName(int c) {
try {
return rsmd.getColumnName(c + 1);
} catch (SQLException e) {
e.printStackTrace();
return "";
}
}
public int getColumnCount() {
try {
return rsmd.getColumnCount();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
public Object getValueAt(int r, int c) {
try {
rs.absolute(r + 1);
return rs.getObject(c + 1);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
@Override
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
public int getRowCount() {
try {
rs.last();
return rs.getRow();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
@Override
public boolean isCellEditable(int r, int c) {
return true;
}
}
}