java - Problems in removing records from database of jList -


hello have problem.. can give me snippet? have table of mysql display jlist item can add item can't remove database? while pressing remove item?

i searched lot no 1 has ever need of doing.. wonder how possible?

private void jbutton3actionperformed(java.awt.event.actionevent evt) {                                                   try {          class.forname("com.mysql.jdbc.driver");            connection con= (connection) drivermanager.getconnection("jdbc:mysql://localhost:3306/test","root","ubuntu123");              preparedstatement stmt = null;          resultset rs =  null;           string [] data;          data = new string[100];          int i=0;           defaultlistmodel listmodel = (defaultlistmodel) jlist2.getmodel();          int selectedindex = jlist2.getselectedindex();          if (selectedindex != -1) {              listmodel.remove(selectedindex);               string query = "delete supplierinfo companyname = ?";              stmt = (preparedstatement) con.preparestatement(query);              stmt.setint(1, i);              stmt.execute();               con.close();               // i= i+1;          }     } catch(exception e) {         system.out.println("3rd catch " +e);     } }                                         

use listmodel#getelementat(int) method selected index.

if model contains string instances, can directly cast string, replace i string in stmt.setint(1, i);


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -