Reputation: 1622
This class shows a list of employees that is a table in mysql, it works, the point is that I want to do an insert or update or delete dynamically in this way:
.
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTable;
import DataBaseConnectionSingleton.Connection;
import DataBaseConnectionSingleton.CreationStatement;
import net.proteanit.sql.DbUtils;
public class ShowEmployee {
public JFrame frame=new JFrame();
JTable Table = new JTable();
JButton b = new JButton();
public ShowEmployee() {
showEmployee();
}
public void showEmployee() {
try {
Connection.getConnectionInstance();
Statement st = CreationStatement.getCreationStatementInstance();
ResultSet rs = st.executeQuery("select * from employee");
frame.getContentPane().setLayout(null);
Table.setBounds(0, 0, 405, 361);
Table.setModel(DbUtils.resultSetToTableModel(rs));
Table.setAutoCreateRowSorter(true);
frame.getContentPane().add(Table);
frame.setTitle("EMPLOYEES'S LIST");
frame.setSize(650,400);
frame.setLocation(265,10);
frame.setVisible(true);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ShowEmployee();
}
}
Upvotes: 0
Views: 1581
Reputation: 658
Yes, you can. I'm not very good in swing but you can do this. Will be lot of work, implementing lot of listeners, changing and reloading table model, repainting the table after data change... very hard. But when you say " to remove the value and insert another in the case of update, or remove the entire row if the delete, or insert a new row" I understand that this wont be done by "clicking a cell". You should implement more controls over the row to know if you want to delete it or insert one above/below and all the operations you want. Hope this helps you, since you just asked if you can do that. Dont you think will be much easier on cell clik to open a new frame over the table and edit the row?
Upvotes: 1