mysql - get more rows in JTable java -
i coded auto suggesting combo boxes. functionality is,
*when user type first letter in either combo box , data retrieves mysql database , show in popup list, when user click on suggested item ,then press add button item added j table , clears combo boxes
but when select item combo box , click add button before added 1 disappears
*how can keep both or many items in j table according above situation *
i'll post code:
private void namecomboactionperformed(java.awt.event.actionevent evt) { string drugname = (string) namecombo.getselecteditem(); try{ string name = "select * druginfo itemname '"+drugname+"%'"; preparedstatement pstmt = conn.preparestatement(name); resultset rs = pstmt.executequery(); while (rs.next()){ idcombo.setselecteditem(rs.getstring("itemid")); } }catch(exception e){ joptionpane.showmessagedialog(null,"error "+ e); } } private void idcomboactionperformed(java.awt.event.actionevent evt) { string drugid = (string) idcombo.getselecteditem(); try{ string name = "select * druginfo itemid '"+drugid+"%'"; preparedstatement pstmt = conn.preparestatement(name); resultset rs = pstmt.executequery(); while (rs.next()){ namecombo.setselecteditem(rs.getstring("itemname")); } }catch(exception e){ joptionpane.showmessagedialog(null,"error "+ e); } try{ string exp = "select expdate druginfo itemid '"+drugid+"%'"; preparedstatement pstmt = conn.preparestatement(exp); resultset rs2 = pstmt.executequery(); while (rs2.next()){ string date = rs2.getstring("expdate"); exptxt.settext(date); } }catch(exception e){ joptionpane.showmessagedialog(null,"error "+ e); } }
add button action performed adding item jtable;
private void add_btnactionperformed(java.awt.event.actionevent evt) { string temp = (string) idcombo.getselecteditem(); string sql = "select itemid,itemname,costprice,instock druginfo itemid=?"; try { pst=conn.preparestatement(sql); pst.setstring(1, temp); rs=pst.executequery(); tablesale.setmodel(dbutils.resultsettotablemodel(rs)); idcombo.setselecteditem(null); namecombo.setselecteditem(null); } catch (exception ex) { joptionpane.showmessagedialog(null, ex); }
add current selection(resultset data) jtable object without replacing old data.
rs=pst.executequery(); adddatatotable(tablesale,dbutils.resultsettotablemodel(rs)); idcombo.setselecteditem(null); namecombo.setselecteditem(null); //add method public void adddatatotable(jtable table,tablemodel model) { defaulttablemodel tablemodel = (defaulttablemodel) table.getmodel(); defaulttablemodel resultsetmodel = (defaulttablemodel) model; (int = 0; < resultsetmodel.getrowcount(); i++) { vector row=new vector(); (int j = 0; j < resultsetmodel.getcolumncount(); j++) { row.addelement(resultsetmodel.getvalueat(i, j)); } tablemodel.addrow(row); } tablemodel.firetabledatachanged(); }
Comments
Post a Comment