单击按钮刷新JTable

时间:2013-02-21 15:18:15

标签: java swing jtable

我有保存功能,当点击时数据保存在数据库中,我在同一个模块中有JTable,但保存时,表格没有显示该数据。我使用了以下代码。

public void save() {
    int n = JOptionPane.showConfirmDialog(frame,
            "Would you like to save the record?", "Confirm",
            JOptionPane.YES_NO_OPTION);

    if (n == JOptionPane.YES_OPTION) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(dbUrl, "root", "");
            Statement stmt = con.createStatement();
            PreparedStatement pstmt = null;
            ResultSet rs;
            String maxId = "Select max(customer_id) from mm_customerrecords";
            rs = stmt.executeQuery(maxId);
            int lastId = 0;
            while (rs.next()) {
                lastId = rs.getInt(1);
            }// to get the last user Id from the database

            int userId = lastId + 1;
            String customer_name = text[1].getText();
            String customer_representativeName = text[2].getText();
            String customer_email = text[3].getText();
            String customer_phno = text[4].getText();
            String customer_address = address.getText();

            String query = "Insert into mm_customerrecords (customer_id,customer_name,customer_representativeName,customer_email,customer_phno,customer_address) VALUES (?,?,?,?,?,?)";
            pstmt = con.prepareStatement(query);
            pstmt.setInt(1,userId);
            pstmt.setString(2, customer_name);
            pstmt.setString(3, customer_representativeName);
            pstmt.setString(4, customer_email);
            pstmt.setString(5, customer_phno);
            pstmt.setString(6, customer_address);
            int check = pstmt.executeUpdate();
            if (check == 1) {
                JOptionPane.showMessageDialog(null,
                        "Data saved successfully !");
                 list.fireTableDataChanged();
            } else {
                JOptionPane.showMessageDialog(null, "Data saving failed !");
            }

            for (int i = 0; i < 5; i++) {
                text[i].setText("");
            }
            address.setText("");

            rs = stmt.executeQuery(maxId);
            int LastId = 0;
            while (rs.next()) {
                LastId = rs.getInt(1);
            }// to get the last user Id from the database

            text[0].setText(Integer.toString(LastId + 1));

            con.close();
            rs.close();
            pstmt.close();

        } // end try

        catch (ClassNotFoundException e) {
            // System.out.println(e);
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(e);
        }

    } else if (n == JOptionPane.NO_OPTION) {

        for (int i = 0; i < 4; i++) {
            text[i].setText("");
        }
        address.setText("");

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(dbUrl, "root", "");
            Statement stmt = con.createStatement();
            String maxId = "Select max(customer_id) from mm_customerrecords";
            ResultSet rs;
            rs = stmt.executeQuery(maxId);
            int lastId = 0;
            while (rs.next()) {
                lastId = rs.getInt(1);
            }// to get the last user Id from the database
            text[0].setText(Integer.toString(lastId + 1));

        } // end try

        catch (ClassNotFoundException e) {
            // System.out.println(e);
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println(e);
        }

        JOptionPane.showMessageDialog(null,
                "You choose not to save the data !");
    }
}

0 个答案:

没有答案