0

I am finding it difficult to update the JTable when I enter a person's name in search button. The program compiles correctly without any errors, but in SearchActionPerformed event when the search button is pressed an empty table comes up. Where without search, the table is full of values from database. In the search criteria, I have typed a surname exactly as one from database but no returns.

public class MainMenu extends javax.swing.JFrame {

        Academic ac = new Academic();
        Connection conn = JavaConnect.ConnectDB();
        PreparedStatement pst = null;
        String sql;
        ResultSet rs = null;
        ImageIcon format = null;
        public byte[] imagedata = null;

        /**
         * Creates new form MainMenu
         */
        public MainMenu() {
            initComponents();
        }

        public void Update_table(String sql) {
            try {
                // String sqlStatement =
                // "SELECT Title, Forename, Surname, Role FROM AcademicInfo ";
                pst = conn.prepareStatement(sql);
                rs = pst.executeQuery();
                table_academic.setModel(DbUtils.resultSetToTableModel(rs));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }

            finally {
                try {
                    rs.close();
                    pst.close();
                } catch (Exception e) {
                }

            }

        }// end method

        private void table_academicMouseClicked(java.awt.event.MouseEvent evt) {
            try {
                int row = table_academic.getSelectedRow();
                String table_click0 = (table_academic.getModel().getValueAt(row, 1)
                        .toString());
                String table_click1 = (table_academic.getModel().getValueAt(row, 2)
                        .toString());
                String sqlSt = "SELECT * FROM AcademicInfo WHERE Forename='"
                        + table_click0 + "' AND Surname='" + table_click1 + "'";
                Connection conn2 = JavaConnect.ConnectDB();
                pst = conn2.prepareStatement(sqlSt);
                rs = pst.executeQuery();

                if (rs.next()) {
                    label_name.setText("" + rs.getString(2) + " " + rs.getString(3)
                            + " " + rs.getString(4));
                    label_role.setText("" + rs.getString(5));
                    label_room.setText("Room:   " + rs.getString(6));
                    jLabel14.setText("Page:   " + rs.getString(7));
                    label_hours.setText("Hours:   " + rs.getString(8));
                    label_phone.setText("Phone:   " + rs.getString(9));
                    label_mobile.setText("Mobile:   " + rs.getString(10));
                    label_email.setText("Email:   " + rs.getString(11));
                    imagedata = (rs.getBytes(12));
                    format = new ImageIcon(imagedata);
                    label_image.setIcon(format);

                }
            }

            catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            } finally {
                try {
                    rs.close();
                    pst.close();
                } catch (Exception e) {
                }
            }
        }

        private void SearchActionPerformed(java.awt.event.ActionEvent evt) {
            try {
                String sql = "SELECT Title, Forename, Surname, Role FROM AcademicInfo WHERE Surname=?";
                PreparedStatement pst1 = conn.prepareStatement(sql);
                pst1.setString(1, search_txt.getText());
                ResultSet rs2 = pst1.executeQuery();
                if (rs2.next()) {

                    JOptionPane.showMessageDialog(null, "Found Academic");
                    Update_table(sql);
                } else {
                    JOptionPane.showMessageDialog(null, "No Results Found");
                }
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    }
2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by mvmalderen
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.