Java XAMPP 连通性
Java XAMPP conectivity
目前,我正在开发 JAVA 程序,它与使用 XAMPP 创建的数据库进行通信。
我使用不同的 JPanel 来显示数据库中的信息,并为用户提供不同的选项来操作数据。
我有一个名为 EmployeePanel 的 class,它扩展了 JPanel,此 class 中的代码从保存在 Employee Table 中的数据库中检索所有信息并显示在 JTable。
当我在 JAVA 程序中处理数据时出现问题。例如,当我调用命令时
(从此 table 中删除一名员工后)
cl.show(cardwrapper, "Employee");
JTable 中的信息未从数据库更新。
这是从数据库中删除条目的代码。
public void DeleteNandoca(){
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, userid, password);
PreparedStatement st = connection.prepareStatement("DELETE FROM `employee` WHERE EMP_FNAME ='"+NandocaToDelete+"'");
st.executeUpdate();
}
catch(Exception e)
{
System.out.println(e);
}
new EmployeePanel();
EmployeePanel.revalidate();
EmployeePanel.repaint();
}
这里是 ActionPerformed 方法的代码(当我想在删除数据库中的条目后显示所有员工时
if (e.getSource() == ListEmployeesMenuItem) {
cl.show(cardwrapper, "Employee");
EmployeePanel.revalidate();
EmployeePanel.repaint();
}
任何人都有想法,我怎样才能让我的 JTable 每次触发 ActionPerformed 方法时都有更新的信息?
应该不需要操作视图组件的封闭容器,EmployeePanel
。相反,将 JTable
留在原位 并更新其 TableModel
;聆听视图将自行更新作为响应。在这个完整的 example 中,Add Row 按钮的 ActionListener
调用 addRow()
,但您可以清除现有行并根据需要添加新行。假设 DefaultTableModel
,使用 setRowCount(0)
清除所有行。
目前,我正在开发 JAVA 程序,它与使用 XAMPP 创建的数据库进行通信。 我使用不同的 JPanel 来显示数据库中的信息,并为用户提供不同的选项来操作数据。
我有一个名为 EmployeePanel 的 class,它扩展了 JPanel,此 class 中的代码从保存在 Employee Table 中的数据库中检索所有信息并显示在 JTable。 当我在 JAVA 程序中处理数据时出现问题。例如,当我调用命令时 (从此 table 中删除一名员工后)
cl.show(cardwrapper, "Employee");
JTable 中的信息未从数据库更新。
这是从数据库中删除条目的代码。
public void DeleteNandoca(){
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, userid, password);
PreparedStatement st = connection.prepareStatement("DELETE FROM `employee` WHERE EMP_FNAME ='"+NandocaToDelete+"'");
st.executeUpdate();
}
catch(Exception e)
{
System.out.println(e);
}
new EmployeePanel();
EmployeePanel.revalidate();
EmployeePanel.repaint();
}
这里是 ActionPerformed 方法的代码(当我想在删除数据库中的条目后显示所有员工时
if (e.getSource() == ListEmployeesMenuItem) {
cl.show(cardwrapper, "Employee");
EmployeePanel.revalidate();
EmployeePanel.repaint();
}
任何人都有想法,我怎样才能让我的 JTable 每次触发 ActionPerformed 方法时都有更新的信息?
应该不需要操作视图组件的封闭容器,EmployeePanel
。相反,将 JTable
留在原位 并更新其 TableModel
;聆听视图将自行更新作为响应。在这个完整的 example 中,Add Row 按钮的 ActionListener
调用 addRow()
,但您可以清除现有行并根据需要添加新行。假设 DefaultTableModel
,使用 setRowCount(0)
清除所有行。