Can i create a method for jTable without using rs2xml.jar?

861 Views Asked by At

I am new to programming and I have coded a program using netbeans 8.1 . My program is for displaying all the relevant items to be searched in a jTable. And then we can select the specific item in another jTable. I used rs2xml to create a method for jTable and it works fine. But after i use it, my searching have gone wrong. It wont display the correct items for the search. when i remove rs2xml.jar from library search works correctly, but when i select the item, it won't display in jTable. I can't figure this out.

here is the code for the search item:

     private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {                                      

    try {

        @SuppressWarnings("LocalVariableHidesMemberVariable")
        ResultSet rs = oilmart.getConnection().createStatement().executeQuery("SELECT * FROM stock WHERE Item_Name LIKE '%" + txtSearch.getText() + "%'");
        if (rs.next()) {

            billinfo();
            txtPlace.setText(rs.getString("Place"));
        } else {

            JOptionPane.showMessageDialog(this, "Result not found", null, JOptionPane.ERROR_MESSAGE, null);

        }

    } catch (SQLException | HeadlessException e) {
    }

    // TODO add your handling code here:
}    

And this is the table method i created using rs2xml.jar:

    public void billinfo() {

    DefaultTableModel dtm = (DefaultTableModel) tblBillinfo.getModel();
    dtm.setRowCount(0);

    try {
        @SuppressWarnings("LocalVariableHidesMemberVariable")
        ResultSet rs = oilmart.getConnection().createStatement().executeQuery("SELECT * FROM stock WHERE Item_Name LIKE '%" + txtSearch.getText() + "%'");

        while (rs.next()) {

            Vector v = new Vector();
            v.add(rs.getString("Item_No"));
            v.add(rs.getString("Item_Name"));
            v.add(rs.getString("Qty"));
            v.add(rs.getString("Price_per_Qty"));
            v.add(rs.getString("Place"));

            buy_price = Integer.parseInt(rs.getString("Price_per_Qty"));

            dtm.addRow(v);
            tblBillinfo.setModel(DbUtils.resultSetToTableModel(rs));
        }

    } catch (Exception e) {
    }

}

And this is for selecting the specific item:

     private void tblBillinfoMouseClicked(java.awt.event.MouseEvent evt) {                                         
    x++;

    int r = tblBillinfo.getSelectedRow();

    String no = tblBillinfo.getValueAt(r, 0).toString();
    String name = tblBillinfo.getValueAt(r, 1).toString();
    String buy = tblBillinfo.getValueAt(r, 3).toString();

    buy_price = (int) tblBillinfo.getValueAt(r, 3);

    String plc = tblBillinfo.getValueAt(r, 4).toString();

    tblBill.setValueAt(no, x, 0);
    tblBill.setValueAt(name, x, 1);
    tblBill.setValueAt(buy, x, 2);
    txtPlace.setText(plc);


}                                        

Please help me to figure this out. Thanks.

1

There are 1 best solutions below

1
camickr On

You have code (which appears to be correct) to add each row of data to the TableModel:

dtm.addRow(v);

But then your very next statement replaces the first row with the remaining rows of data in the ResultSet:

tblBillinfo.setModel(DbUtils.resultSetToTableModel(rs));

The result is that your TableModel will be missing the first row of data.

Just get rid of the above statement.