如何显示数据库中的数据,无需在JTable中附加其他数据库中的其他数据?

时间:2012-03-06 03:37:23

标签: java mysql swing

我有一个JComboBox,它从数据库中检索数据并将其显示在JTable中。除了来自不同数据库的数据在JTable中组合之外,一切都运行良好。您是否知道如何仅显示特定数据库的数据?谢谢!

以下是我使用的代码。

  private void comBoxActionPerformed(java.awt.event.ActionEvent evt)  
  {                                       
        if (comBox.getSelectedItem() == "Select . . .") {   

        } else if (comBox.getSelectedItem() == "Students") {
        DefaultTableModel model = (DefaultTableModel) dataTable.getModel();  
        String sql = "select * from students";
        try {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(tests.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection connect = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/datavisibility","root","");
        Statement statmnt = connect.createStatement();
        ResultSet rslt = statmnt.executeQuery(sql);
        while(rslt.next()){
            String id = rslt.getString("ID");
            String name = rslt.getString("Name");
            String surname = rslt.getString("Surname");
            String age = rslt.getString("Age");
            model.addRow(new Object[]{id,name,surname,age});
        }
    } catch(SQLException e){
        e.printStackTrace();
    }
    } else if (comBox.getSelectedItem() == "Teachers"){
    DefaultTableModel model = (DefaultTableModel) dataTable.getModel();
    String sql = "select * from teachers";
    try {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(tests.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection connect = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/datavisibility","root","");
        Statement statmnt = connect.createStatement();
        ResultSet rslt = statmnt.executeQuery(sql);
        while(rslt.next()){
            String id = rslt.getString("ID");
            String name = rslt.getString("Name");
            String surname = rslt.getString("Surname");
            String age = rslt.getString("Age");
            model.addRow(new Object[]{id,name,surname,age});

        }
    } catch(SQLException e){
        e.printStackTrace();
    }
    } else if (comBox.getSelectedItem() == "Directors") {
    DefaultTableModel model = (DefaultTableModel) dataTable.getModel();
    String sql = "select * from directors";
    try {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(tests.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection connect = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/datavisibility","root","");
        Statement statmnt = connect.createStatement();
        ResultSet rslt = statmnt.executeQuery(sql);
        while(rslt.next()){
            String id = rslt.getString("ID");
            String name = rslt.getString("Name");
            String surname = rslt.getString("Surname");
            String age = rslt.getString("Age"); 
            model.addRow(new Object[]{id,name,surname,age});

        }
    } catch(SQLException e){
        e.printStackTrace();
    }
    }
}                                 `

这是该计划的output

我将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

您正在调用model.addRow(),它将在现有数据模型上添加新行。它不是在任何地方清除数据模型。因此,行将被附加到先前的数据。您需要将数据模型设置为新模型或首先删除现有数据。

答案 1 :(得分:0)

您必须首先清除行,如Ashwini的答案。看看这个框架,使编码更容易。它有你的表问题的答案。基本上,您应该构建模型而不是使用DefaultTableModel并为其添加其他功能。

http://code.google.com/p/swingobjects/

请看那里描述Swing对象表模型使用的第二个例子。