修改这段代码实现选择表格一行信息删除package frame; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; //添加页面 public class EditDialog extends BaseDialog{ DataChangelisenter dataChangelisenter; public void setDataChangelisenter(DataChangelisenter dataChangelisenter) { this.dataChangelisenter = dataChangelisenter; } @Override public void init() { setLayout(new FlowLayout()); //布局 setTitle("修改图书信息"); JPanel jp0=new JPanel(); JLabel l0 = new JLabel("id"); JTextField t0 = new JTextField(); t0.setPreferredSize(new Dimension(300,30)); jp0.add(l0); jp0.add(t0); JPanel jp1=new JPanel(); JLabel l1 = new JLabel("书名"); JTextField t1 = new JTextField(); t1.setPreferredSize(new Dimension(300,30)); jp1.add(l1); jp1.add(t1); JPanel jp2=new JPanel(); JLabel l2 = new JLabel("作者"); JTextField t2 = new JTextField(); t2.setPreferredSize(new Dimension(300,30)); jp2.add(l2); jp2.add(t2); JPanel jp3=new JPanel(); JLabel l3 = new JLabel("分类"); JTextField t3 = new JTextField(); t3.setPreferredSize(new Dimension(300,30)); jp3.add(l3); jp3.add(t3); JPanel jp4=new JPanel(); JLabel l4 = new JLabel("出版社"); JTextField t4 = new JTextField(); t4.setPreferredSize(new Dimension(300,30)); jp4.add(l4); jp4.add(t4); JPanel jp5=new JPanel(); JLabel l5 = new JLabel("数量"); JTextField t5 = new JTextField(); t5.setPreferredSize(new Dimension(300,30)); jp5.add(l5); jp5.add(t5); add(jp0); add(jp1); add(jp2); add(jp3); add(jp4); add(jp5); JButton b1 = new JButton("保存"); b1.setPreferredSize(new Dimension(150,30)); b1.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { String id = t0.getText(); String sname = t1.getText(); String author = t2.getText(); String classify = t3.getText(); String press = t4.getText(); String quantity = t5.getText(); Connection con = DBUtil.getConnect(); String sql = "UPDATE tushu SET sname=?,author=?,classify=?,press=?,quantity=? WHERE id=?"; try { PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,sname); ps.setString(2,author); ps.setString(3,classify); ps.setString(4,press); ps.setString(5,quantity); ps.setString(6,id); int result = ps.executeUpdate(); if (result>0){ JOptionPane.showMessageDialog(EditDialog.this,"修改成功"); // new StudentFrame(); dataChangelisenter.dataChange(); dispose(); }else { JOptionPane.showMessageDialog(EditDialog.this,"修改失败"); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }); add(b1); } }
时间: 2024-01-02 15:03:45 浏览: 184
java计算器.pdf
修改后的代码如下:
```
package frame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class EditDialog extends BaseDialog{
DataChangelisenter dataChangelisenter;
JTable table;
public void setDataChangelisenter(DataChangelisenter dataChangelisenter) {
this.dataChangelisenter = dataChangelisenter;
}
public void setTable(JTable table) {
this.table = table;
}
@Override
public void init() {
setLayout(new FlowLayout()); // 布局
setTitle("修改图书信息");
JPanel jp0 = new JPanel();
JLabel l0 = new JLabel("id");
JTextField t0 = new JTextField();
t0.setPreferredSize(new Dimension(300, 30));
jp0.add(l0);
jp0.add(t0);
JPanel jp1 = new JPanel();
JLabel l1 = new JLabel("书名");
JTextField t1 = new JTextField();
t1.setPreferredSize(new Dimension(300, 30));
jp1.add(l1);
jp1.add(t1);
JPanel jp2 = new JPanel();
JLabel l2 = new JLabel("作者");
JTextField t2 = new JTextField();
t2.setPreferredSize(new Dimension(300, 30));
jp2.add(l2);
jp2.add(t2);
JPanel jp3 = new JPanel();
JLabel l3 = new JLabel("分类");
JTextField t3 = new JTextField();
t3.setPreferredSize(new Dimension(300, 30));
jp3.add(l3);
jp3.add(t3);
JPanel jp4 = new JPanel();
JLabel l4 = new JLabel("出版社");
JTextField t4 = new JTextField();
t4.setPreferredSize(new Dimension(300, 30));
jp4.add(l4);
jp4.add(t4);
JPanel jp5 = new JPanel();
JLabel l5 = new JLabel("数量");
JTextField t5 = new JTextField();
t5.setPreferredSize(new Dimension(300, 30));
jp5.add(l5);
jp5.add(t5);
add(jp0);
add(jp1);
add(jp2);
add(jp3);
add(jp4);
add(jp5);
JButton b1 = new JButton("保存");
b1.setPreferredSize(new Dimension(150, 30));
b1.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
String id = t0.getText();
String sname = t1.getText();
String author = t2.getText();
String classify = t3.getText();
String press = t4.getText();
String quantity = t5.getText();
Connection con = DBUtil.getConnect();
String sql = "UPDATE tushu SET sname=?,author=?,classify=?,press=?,quantity=? WHERE id=?";
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,sname);
ps.setString(2,author);
ps.setString(3,classify);
ps.setString(4,press);
ps.setString(5,quantity);
ps.setString(6,id);
int result = ps.executeUpdate();
if (result > 0){
JOptionPane.showMessageDialog(EditDialog.this,"修改成功");
// 删除选中的行
int selectedRow = table.getSelectedRow();
DefaultTableModel model = (DefaultTableModel) table.getModel();
if (selectedRow != -1) {
model.removeRow(selectedRow);
}
dataChangelisenter.dataChange();
dispose();
}else {
JOptionPane.showMessageDialog(EditDialog.this,"修改失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
});
add(b1);
}
}
```
在这个代码中,我们添加了一个 `setTable` 方法,用于设置需要操作的表格对象。然后,在保存按钮的事件监听器中,我们通过 `table.getSelectedRow()` 来获取当前选中的行,然后使用 `DefaultTableModel` 的 `removeRow` 方法来删除该行数据。最后,我们调用 `dataChangelisenter.dataChange()` 方法来通知主界面数据已经发生了改变。
阅读全文