要使用JTable修改数据到数据库,您需要遵循以下步骤:
1. 获取选定的行和列的数据:使用JTable的`getSelectedRow()`和`getSelectedColumn()`方法获取选定的行和列的索引。然后使用JTable的`getValueAt()`方法获取选定的单元格的值。
2. 连接数据库:使用JDBC或其他数据库连接方式连接数据库。
3. 更新数据库中的数据:根据选定的行和列的数据,构建适当的SQL语句,并使用JDBC的Statement或PreparedStatement对象将数据更新到数据库中。
以下是一个示例代码,演示如何使用JTable修改数据到数据库:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; public class JTableExample extends JFrame {????private?JTable?table;
????private?JButton?updateButton;
????private?Connection?connection;
????public?JTableExample()?{
????????//?创建JTable和按钮
????????table?=?new?JTable();
????????updateButton?=?new?JButton(“Update”);
????????//?添加按钮点击事件的监听器
????????updateButton.addActionListener(new?ActionListener()?{
????????????@Override
????????????public?void?actionPerformed(ActionEvent?e)?{
????????????????updateData();
????????????}
????????});
????????//?添加JTable和按钮到窗口
????????setLayout(new?BorderLayout());
????????add(new?JScrollPane(table),?BorderLayout.CENTER);
????????add(updateButton,?BorderLayout.SOUTH);
????????//?连接数据库并加载数据到JTable
????????try?{
????????????connection?=?DriverManager.getConnection(“jdbc:mysql://localhost:3306/ ????????????mydatabase”,?“username”,?“password”);
????????????Statement?statement?=?connection.createStatement();
????????????ResultSet?resultSet?=?statement.executeQuery(“SELECT?*?FROM?mytable”);
????????????ResultSetMetaData?metaData?=?resultSet.getMetaData();
????????????//?创建一个DefaultTableModel来存储数据库中的数据
????????????DefaultTableModel?model?=?new?DefaultTableModel();
????????????//?添加表头
????????????int?columnCount?=?metaData.getColumnCount();
????????????for?(int?i?=?1;?i?<=?columnCount;?i++)?{
????????????????model.addColumn(metaData.getColumnName(i));
????????????}
????????????//?添加数据行
????????????while?(resultSet.next())?{
????????????????Object[]?rowData?=?new?Object[columnCount];
????????????????for?(int?i?=?1;?i?<=?columnCount;?i++)?{
????????????????????rowData[i?-?1]?=?resultSet.getObject(i);
????????????????}
????????????????model.addRow(rowData);
????????????}
????????????//?将DefaultTableModel设置为JTable的数据模型
????????????table.setModel(model);
????????}?catch?(SQLException?e)?{
????????????e.printStackTrace();
????????}
????????//?设置窗口属性
????????setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
????????pack();
????????setLocationRelativeTo(null);
????????setVisible(true);
????}
????private?void?updateData()?{
????????int?selectedRow?=?table.getSelectedRow();
????????int?selectedColumn?=?table.getSelectedColumn();
????????Object?selectedValue?=?table.getValueAt(selectedRow,?selectedColumn);
????????//?构建更新数据的SQL语句
????????String?updateSql?=?“UPDATE?mytable?SET?"?+?table.getColumnName(selectedColumn) ?????????+?"?=???WHERE?id?=??”;
????????try?{
????????????PreparedStatement?preparedStatement?=?connection.prepareStatement(updateSql);
????????????preparedStatement.setObject(1,?selectedValue);
????????????preparedStatement.setInt(2,?(int)?table.getValueAt(selectedRow,?0));
????????????preparedStatement.executeUpdate();
????????????//?更新JTable的数据模型
????????????DefaultTableModel?model?=?(DefaultTableModel)?table.getModel();
????????????model.setValueAt(selectedValue,?selectedRow,?selectedColumn);
????????}?catch?(SQLException?e)?{
????????????e.printStackTrace();
????????}
????}
????public?static?void?main(String[]?args)?{
????????SwingUtilities.invokeLater(new?Runnable()?{
????????????@Override
????????????public?void?run()?{
????????????????new?JTableExample();
????????????}
????????});
????} }
请注意替换示例代码中的数据库连接URL、用户名和密码,以及在updateData()
方法中的SQL语句和表名,以适应您的数据库和表结构。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: druid数据库连接池阻塞如何解决