dimanche 29 mai 2016

Java: Refresh a JTable in a if-loop using PreparedStatement (sql)

I'm trying to make a research window that would be able to find an information in a PostgreSql database using Phone number OR name of the person I look for. My problem is that my JTable is in a If-loop ( To check if the user search with the name or the phone number ) so if the user use the window to search two different people, my code will add a new JTable below the first one instead of refreshing it.

Here is the ActionListener that cause my problem.

        class OkListener implements ActionListener {
        public void actionPerformed(ActionEvent arg0){
            try {

            String name = nameField.getText();
            String phone = phoneField.getText();
            Connection conn = CCConnection.getInstance();
            if (!phone.isEmpty() && name.isEmpty() ){


                String sqlName = "select * from creditcopie where telephone = ?";
                PreparedStatement preparedStatementName = CCConnection.getInstance().prepareStatement(sqlName, ResultSet.TYPE_SCROLL_SENSITIVE, 
                        ResultSet.CONCUR_UPDATABLE);
                preparedStatementName.setString(1, phone);
                ResultSet rs = preparedStatementName.executeQuery();
                JTable table = new JTable(buildTableModel(rs));
                container.add(new JScrollPane(table));
                container.revalidate();
                container.repaint();


            }

            else if (phone.isEmpty() && !name.isEmpty() ){
                String sqlName = "select * from creditcopie where nom = ?";
                PreparedStatement preparedStatementName = CCConnection.getInstance().prepareStatement(sqlName, ResultSet.TYPE_SCROLL_SENSITIVE, 
                        ResultSet.CONCUR_UPDATABLE);
                preparedStatementName.setString(1, name);
                ResultSet rs = preparedStatementName.executeQuery();
                JTable table = new JTable(buildTableModel(rs));
                container.add(new JScrollPane(table));
                container.revalidate();
                container.repaint();
                confirmButton.setEnabled(false);

            }

            else{
                    JOptionPane.showMessageDialog(null, "Unable to do the research", "Error! ", JOptionPane.WARNING_MESSAGE);   
            }



            } catch (SQLException e) {

                e.printStackTrace();
            }


    }

And this is the buildTableModel

            public DefaultTableModel buildTableModel(ResultSet rs)
                throws SQLException {

            ResultSetMetaData metaData = rs.getMetaData();

            // names of columns
            Vector<String> columnNames = new Vector<String>();
            int columnCount = metaData.getColumnCount();
            for (int column = 1; column <= columnCount; column++) {
                columnNames.add(metaData.getColumnName(column));
            }

            // data of the table
            Vector<Vector<Object>> data = new Vector<Vector<Object>>();
            while (rs.next()) {
                Vector<Object> vector = new Vector<Object>();
                for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                    vector.add(rs.getObject(columnIndex));
                }
                data.add(vector);
            }

            return new DefaultTableModel(data, columnNames);

        }

Aucun commentaire:

Enregistrer un commentaire