java - how to get multiple values from one text field with delimiter and save each to database -
actually have 10-30 dummies value txtcc
, i'd used 3 dummies example below..
so how each values , save directly database without using dummy? it's big deal coz' code large compile using dummies..
thanks help..
private void bsaveactionperformed(java.awt.event.actionevent evt) { // save database string cc = txtcc.gettext(); string delimiter = ","; string[] temp; temp = cc.split(delimiter); for(int = 0; < temp.length; i++) if(i==0) { txtc1.settext(temp[0]); txtc2.settext("0"); txtc3.settext("0"); } else if (i==1) { txtc1.settext(temp[0]); txtc2.settext(temp[1]); txtc3.settext("0"); } else if (i==2) { txtc1.settext(temp[0]); txtc2.settext(temp[1]); txtc3.settext(temp[2]); } try { string cc1 = txtc1.gettext(); int cc1 = integer.parseint(cc1); string cc2 = txtc2.gettext(); int cc2 = integer.parseint(cc2); string cc3 = txtc3.gettext(); int cc3 = integer.parseint(cc3); int opt = joptionpane.showconfirmdialog(null,"are sure want save record? "); if (opt == 0){ if(!txtc1.gettext().equals("0")) { stmt=con.createstatement(resultset.type_scroll_sensitive, resultset.concur_updatable); string sql = "select * tbl_liqinfo"; rs = stmt.executequery(sql); rs.next(); rs.movetoinsertrow(); rs.updateint("cc", cc1); rs.insertrow(); rs.close(); } if(!txtc2.gettext().equals("0")) { stmt=con.createstatement(resultset.type_scroll_sensitive, resultset.concur_updatable); string sql = "select * tbl_liqinfo"; rs = stmt.executequery(sql); rs.next(); rs.movetoinsertrow(); rs.updateint("cc", cc2); rs.insertrow(); rs.close(); } if(!txtc3.gettext().equals("0")) { stmt=con.createstatement(resultset.type_scroll_sensitive, resultset.concur_updatable); string sql = "select * tbl_liqinfo"; rs = stmt.executequery(sql); rs.next(); rs.movetoinsertrow(); rs.updateint("cc", cc3); rs.insertrow(); rs.close(); } } } catch (sqlexception err){ joptionpane.showmessagedialog(frmempliquidation.this, err.getmessage()); } }
instead of using dummies, create simple small methods , make use of it. reduce line of code. , easy understand.
private void bsaveactionperformed(java.awt.event.actionevent evt){
// save database
string cc = txtcc.gettext(); string delimiter = ","; string[] temp; temp = cc.split(delimiter); for(int = 0; < temp.length; i++) insertdata(temp[i]); } public void insertdata(final string data){ txtc1.settext(data); try { string cc1 = txtc1.gettext(); int cc1 = integer.parseint(cc1); int opt = joptionpane.showconfirmdialog(null,"are sure want save record? "); if (opt == 0){ if(!txtc1.gettext().equals("0")) { stmt=con.createstatement(resultset.type_scroll_sensitive, resultset.concur_updatable); string sql = "select * tbl_liqinfo"; rs = stmt.executequery(sql); rs.next(); rs.movetoinsertrow(); rs.updateint("cc", cc1); rs.insertrow(); rs.close(); } } } catch (sqlexception err){ joptionpane.showmessagedialog(frmempliquidation.this, err.getmessage()); } }
Comments
Post a Comment