java - Select a item from a JLIST and update a form with all db info from that selected id -
my question similar this question, didn't work me =(
i have db named clients id,name,phone , email. when register client, name , id shown on jlist. want click on name, inside jlist , "search" data client (id, name, phone , email). have textfield´s link data, isn't working...
what did:
defaultlistmodel model = new defaultlistmodel(); client_listclientes.setmodel(model); try { string host = "jdbc:derby://localhost:1527/anaia_db"; string uname = "*****"; string upass = "*****"; con = drivermanager.getconnection(host, uname, upass); stmt = con.createstatement(); string sql = "select * app.clients"; int id_col = rs.getint("clientid"); // need make work.. when select value, gives me id client resultset rs = stmt.executequery("select * app.clients clientid='"+id_col+"'"); rs = stmt.executequery(sql); while (rs.next()){ string name = rs.getstring("clientname"); client_textname.settext(name); string phone = rs.getstring("clientphone"); client_textphone.settext(phone); string email = rs.getstring("clientmail"); client_textmail.settext(email); } } catch (sqlexception err) { joptionpane.showmessagedialog(main.this, err.getmessage()); } i read have use listener, tried exemple other question , didnt work =/
just load data client object @ 1 time, instead of trying go , hit db each time.
class client { int id; string name; string phone; string email; // getter , setters @override public string tostring() { return id + " : " + name; } } then can can load data client objects , add them model
resultset rs = stmt.executequery("select * app.clients"); ... while (rs.next()){ client client = new client(); client.setid(rs.getint("id"); client.setname(rs.getstring("clientname")); client.setphone(rs.getstring("clientphone")); client.setemail(rs.getstring("clientmail"); model.addelement(client); } then wehn click on jlist, extract client object selected , use getters set text fields
client_listclientes.addlistselectionlistener(new listselectionlistener(){ public void valuechanged(listselectionevent e) { if (!e.getvalueisadjusting()) { jlist list = (jlist)e.getsource(); client client = (client)list.getselectedvalue(); textfield.settext(client.getemail()); } } });
Comments
Post a Comment