1

How to add new rows into a jTable from database while button click without clearing existing rows in jTable?

I tried lot of ways. But no success. Help

String SQL = "SELECT name,price FROM items WHERE ID = ' "+jTextField1.getText()+" ' ";
pst = Conn.prepareStatement(sql);
rs = pst.executeQuery();
jTable1.setModel(DbUnits.resultSetToTableModel(rs));

Edit:- With the help of all answers i change the code into below code, But OK . But here i get a error in DefaultTableModel

String sql = "SELECT name,price FROM items WHERE ID = '"+jtxt1.getText()+"'";
        pst = conn.prepareStatement(sql);
        rs=pst.executeQuery();
DefaultTableModel model = new DefaultTabelModel(new String[]{"Name","Price"},0);
        Vector row = new Vector();
        while(rs.next())
        {
        String d = rs.getString("name");
        String e = rs.getString("price");
        row.add(new Object[]{d,e});

        model.addRow(row);}

New Code

String sql = "SELECT name,price FROM items WHERE ID = '"+jtxt1.getText()+"'";
        pst = conn.prepareStatement(sql);
        rs=pst.executeQuery();
        DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
Vector row = new Vector();
row.add(rs);
model.addRow(row);

Newest Code

ResultSetMetaData metaData;
public void metaData() throws SQLException {
        this.metaData = rs.getMetaData();
        }
String sql = "SELECT name,price FROM items WHERE ID = '"+jtxt1.getText()+"'";
            pst = conn.prepareStatement(sql);
            rs=pst.executeQuery();

            int columnCount = metaData.getColumnCount();
            Vector<String> columnNames = new Vector<String>();

            for (int column = 1; column <= columnCount; column++) {
                columnNames.add(metaData.getColumnName(column));
                System.out.println("ColumnNames "+columnNames );
            }

            DefaultTableModel datamodel = new DefaultTableModel(columnNames, 0);
            jTable1.setModel(datamodel);

            while (rs.next()) {
                Vector<String> vector = new Vector<String>();
                for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getString(columnIndex)); 
                }
                datamodel.addRow(vector);
            }

**But here i get a NullpointException error ! And how do i select jTable1 as the table in above code ?

7
  • 1) Why are you adding an Array to the Vector? I sure hope this is NOT code you found in an accepted answer in the forum. I would like to see the link were you found this code? You should just be adding the name and price directly to the Vector. 2) I get an error in DefaultTableModel - we are not mind readers, we don't know what the error is. Again, did you search the forum for the error message? 3) why are you creating a new DefaultTableModel. Your question is about adding new rows to the existing table model. So you only need a reference to the model so you can use the addRow(...) method. Commented Sep 22, 2017 at 3:33
  • Yes. Sorry for disturbing. :\ New code what is the error ? Commented Sep 22, 2017 at 4:31
  • I give up you haven't listened to any advice given. 1) you still haven't posted the error message. 2) you have been told you need to loop through the ResultSet and add one row of data at a time. Your first edit has the loop, the second edit doesn't. 3) you have bee told to add the "name" and "price" to the Vector (not the ResultSet). 4) you have been told to search the forum for examples. There are plenty of example that show how to add data to a Vector. It would appear you also need to buy a programming text book. This is basic Java that you should know before doing database programming. Commented Sep 22, 2017 at 13:58
  • Error - not showing anything in the jTable, Commented Sep 23, 2017 at 2:18
  • But it works as i wanted!(Can add row by row) (But nothing showing in that rows. Commented Sep 23, 2017 at 2:19

1 Answer 1

1

Process each row of data from the ResultSet and create a Vector and use this method to insert the data into the table model. You are creating new table model and setting it on the table, the old model with the data is lost.

After below request in comment:

This is one way to do it.

Vector<Vector<String>> data=new Vector<>();
//Fill this Vector above with the initial data

Vector<String> columns=new Vector<String>();
//Fill this with column names

DefaultTableModel tableModel=new DefaultTableModel(data, columns);
JTable table=new JTable(tableModel);
//Display the table as you like

... //Query the database and get the ResultSet (let's call it rs)

while(rs.next){

  Vector<String> newRow=new Vector<>();

  //Get the data from the resultset and fill this new row

  tableModel.addRow(newRow);

}//while closing
Sign up to request clarification or add additional context in comments.

6 Comments

Pls. Explain it ! Step by step.
OK Thanks. But I'm not good at Java. So pls can you modify my code into correct code. Please!
What is your objective here, is it some kind of college project?
I'm trying to develop a POS system(Billing, Inventory..) .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.