Start New Discussion within our Software Development Community

Hi Guys,

I managed to fill the jtable with the table from the database.

Now I want to add specific rows from the database table into the jtable.

So I have a textfield and a sql statement: So "Select * from tbl where name =?"

I want to add all the entries searched for from the textfield to the jtable. I am not sure it make sense?

Any idea how I can achieve that?

I was thinking of storing all the entries types in to another database table and then display the results but I think there could be an easier solution?

So Far I have:

String val = input.getText();

         Properties conProps = new Properties();
        conProps.setProperty("user", "root");
        conProps.setProperty("password", "");
 try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", conProps);
            String sql = ("SELECT * FROM Students where Name ='"+val+"' ");
            st = (com.mysql.jdbc.PreparedStatement) con.prepareStatement(sql);
            st.executeQuery();

            rs = st.getResultSet();
            Table_Stud.setModel(DbUtils.resultSetToTableModel(rs));


            } catch (Exception e) {
            e.printStackTrace();


            }
         DefaultTableModel model = (DefaultTableModel) Table_Stud.getModel();
model.addRow(new Object[]{val});

The values searched for are displayed in the table(Table_Stud) but once I type something else in the textfield(input) the results are removed and searched for a new entry.

This question has already been answered. Start a new discussion instead.