JButton updateButton = new JButton("修改"); updateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int selectedRow = table.getSelectedRow(); if (selectedRow != 0) { int id = (int) table.getValueAt(selectedRow, 0); String name = nameTextField.getText(); String publisher = publisherTextField.getText(); int stock = Integer.parseInt(stockTextField.getText()); Book book = new Book(id, name, publisher, stock); bookManager.updateBook(book); refreshTable(); } } }); panel.add(updateButton);public void updateBook(Book book) { try { // 执行更新语句 String sql = "UPDATE books SET name = ?, publisher = ?, stock = ? WHERE id = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, book.getName()); statement.setString(2, book.getPublisher()); statement.setInt(3, book.getStock()); statement.setInt(4, book.getId()); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } }这两段代码有什么错误吗
时间: 2024-02-14 17:31:32 浏览: 72
这两段代码看起来没有明显的语法错误。不过,可能存在一些潜在的问题需要注意。首先,确保在使用`updateButton`之前已经创建了`nameTextField`、`publisherTextField`和`stockTextField`。此外,还要确保`table`和`bookManager`的定义和初始化正确。还有可能出现的问题是数据库连接是否正确建立和关闭。
另外,需要确保在更新书籍之前,已经从`table`中选择了一行。如果没有选择任何行(即`selectedRow`为-1),那么代码块中的更新操作将不会执行。
如果还有其他问题,请提供更多的上下文信息或具体的错误提示,以便更好地帮助您解决问题。
相关问题
优化这段代码package com.daiinfo.catering.pane; import com.daiinfo.catering.util.JDBConnection; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.EventListener; public class DishManagePane2 extends JFrame { private JTable table; private JButton addButton,updateButton,deleteButton; public DishManagePane2(){ setTitle("菜品管理"); setSize(800,600); String [] columnNames = {"菜品名","价格","菜品类别"}; Object[][] data = {{"糖醋排骨",29.0,"西餐"},{"宫保鸡丁",28.0,"中餐"}}; JTable table = new JTable(data,columnNames); addButton = new JButton("增加"); updateButton = new JButton("修改"); deleteButton = new JButton("删除"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { } }); JPanel panel = new JPanel(new BorderLayout()); panel.add(new JScrollPane(table),BorderLayout.CENTER); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(addButton); buttonPanel.add(updateButton); buttonPanel.add(deleteButton); panel.add(buttonPanel,BorderLayout.SOUTH); add(panel); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new JDBConnection(); new DishManagePane2(); } }
以下是对代码的优化:
1. 将 JTable、JButton 声明为类成员变量,方便后面的方法访问。
2. 将 JTable 中的数据从构造函数中移除,改为从数据库中读取,使用 JDBConnection 类进行数据库连接和查询。
3. 将按钮的 ActionListener 单独提取出来,作为类内部的一个私有方法。
4. 将 JFrame 窗口的设置移到单独的方法中,方便对窗口属性的修改和扩展。
优化后的代码如下:
```java
package com.daiinfo.catering.pane;
import com.daiinfo.catering.util.JDBConnection;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class DishManagePane2 extends JFrame {
private JTable table;
private JButton addButton, updateButton, deleteButton;
public DishManagePane2() {
setTitle("菜品管理");
setSize(800, 600);
setWindow();
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(table), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
add(panel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void setWindow() {
addButton = new JButton("增加");
updateButton = new JButton("修改");
deleteButton = new JButton("删除");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 添加菜品的逻辑
}
});
// 从数据库中读取数据并绑定到 JTable 上
try {
Connection conn = JDBConnection.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from dishes";
ResultSet rs = stmt.executeQuery(sql);
Vector<Vector<Object>> data = new Vector<>();
while (rs.next()) {
Vector<Object> row = new Vector<>();
row.add(rs.getString("name"));
row.add(rs.getDouble("price"));
row.add(rs.getString("category"));
data.add(row);
}
Vector<String> columnNames = new Vector<>();
columnNames.add("菜品名");
columnNames.add("价格");
columnNames.add("菜品类别");
table = new JTable(data, columnNames);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new JDBConnection();
new DishManagePane2();
}
}
```
这样,代码就更加清晰、易于维护和扩展了。
帮我根据以下要求:Then modify the View superclass to: hide the frame when the user clicks on the “close” button; add a “window closing” event handler (use an anonymous window adapter) that calls the controller’s shutdown method.修改下述代码:import javax.swing.JFrame; public abstract class View<T extends Controller> extends JFrame implements ModelListener { protected Model m; protected T c; public View(Model m, T c) { this.m = m; this.c = c; m.addListener(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public abstract void update(); },public class MyFrame extends View<ControllerClicks> { public MyFrame(Model m, ControllerClicks c) { super(m, c); this.setTitle("MyFrame Title"); this.setSize(400, 300); this.setLocationRelativeTo(null); this.setLayout(new BorderLayout()); MyPanel centerPanel = new MyPanel(m, c); this.add(centerPanel, BorderLayout.CENTER); JPanel topPanel = new JPanel(); this.add(topPanel, BorderLayout.PAGE_START); topPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { c.resetClicked(); } }); topPanel.add(resetButton); JButton undoButton = new JButton("Undo"); undoButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { c.undoClicked(); } }); topPanel.add(undoButton); this.setVisible(true); } @Override public void update() { repaint(); // Makes Swing call MyPanel's paintComponent method. } } import javax.swing.JLabel; public class ViewNumber extends View<Controller> { private JLabel label; public ViewNumber(Model m, Controller c) { super(m, c); this.setTitle("View Number"); this.setSize(150, 150); label = new JLabel(); update(); // Initialize the label using the model. this.add(label); this.setVisible(true); } @Override public void update() { label.setText("Number of points is: " + m.numberOfPoints()); } }
修改后的代码如下:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public abstract class View<T extends Controller> extends JFrame implements ModelListener {
protected Model m;
protected T c;
public View(Model m, T c) {
this.m = m;
this.c = c;
m.addListener(this);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
c.shutdown();
}
});
}
@Override
public abstract void update();
}
public class MyFrame extends View<ControllerClicks> {
public MyFrame(Model m, ControllerClicks c) {
super(m, c);
this.setTitle("MyFrame Title");
this.setSize(400, 300);
this.setLocationRelativeTo(null);
this.setLayout(new BorderLayout());
MyPanel centerPanel = new MyPanel(m, c);
this.add(centerPanel, BorderLayout.CENTER);
JPanel topPanel = new JPanel();
this.add(topPanel, BorderLayout.PAGE_START);
topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.resetClicked();
}
});
topPanel.add(resetButton);
JButton undoButton = new JButton("Undo");
undoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
c.undoClicked();
}
});
topPanel.add(undoButton);
this.setVisible(true);
}
@Override
public void update() {
repaint(); // Makes Swing call MyPanel's paintComponent method.
}
}
import javax.swing.JLabel;
public class ViewNumber extends View<Controller> {
private JLabel label;
public ViewNumber(Model m, Controller c) {
super(m, c);
this.setTitle("View Number");
this.setSize(150, 150);
label = new JLabel();
update(); // Initialize the label using the model.
this.add(label);
this.setVisible(true);
}
@Override
public void update() {
label.setText("Number of points is: " + m.numberOfPoints());
}
}
阅读全文