要使用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語句和表名,以適應您的數據庫和表結構。