java - Display data on JTable from Mysql db after click off button -
i've been trying insert data mysql database , @ same time display data in jtable. problem unable store data database able display in jtable. have placed db connection string in class follows
public class databaseconnection { public static connection getdbconnection() { connection connection; try { class.forname("com.mysql.jdbc.driver").newinstance(); connection = drivermanager.getconnection("jdbc:mysql://localhost:3306/accounting","root","jinxed007"); return connection; } catch (exception ex) { return null; } }
}
and method implementing jtable follows
table = new jtable(dtablemodel); try{ class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/accounting","root","jinxed007"); statement sqlstate = conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable); string selectstuff = "select customerid,name,address,town,postal_code,tel_no,contact_person customer"; rows = sqlstate.executequery(selectstuff); object[] temprow; while (rows.next()) { temprow = new object[] { rows.getint(1), rows.getstring(2), rows.getstring(3), rows.getstring(4), rows.getstring(5) ,rows.getint(6),rows.getstring(7)}; dtablemodel.addrow(temprow); } } catch (sqlexception ex) { // string describing error system.out.println("sqlexception: " + ex.getmessage()); // vendor specific error code system.out.println("vendorerror: " + ex.geterrorcode()); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } table.setautocreaterowsorter(true); table.setfont(new font("segoe ui", font.plain, 12)); table.setborder(new etchedborder(etchedborder.raised, null, null)); scrollpane.setviewportview(table);
while actionlistener follows
btnsave.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent arg0) { string scustomerid="",scustomername="",saddress="",scity="",spostalcode="",stelno="",scontactperson=""; scustomerid=txtcustomernumber.gettext(); scustomername=txtcustomername.gettext(); saddress=txtaddress.gettext(); scity=txtcity.gettext(); spostalcode=txtpostalcode.gettext(); stelno=txttelno.gettext(); scontactperson=txtcontactperson.gettext(); try { // moves database row data placed rows.movetoinsertrow(); // update values in database rows.updatestring("customerid", scustomerid); rows.updatestring("name", scustomername); rows.updatestring("address", saddress); rows.updatestring("town", scity); rows.updatestring("postal_code", spostalcode); rows.updatestring("tel_no", stelno); rows.updatestring("contact_person",scontactperson ); // inserts changes row values in database rows.insertrow(); // directly updates values in database rows.updaterow(); } catch (sqlexception e1) { e1.printstacktrace(); } //int scustomerid = 0; try { // go last row inserted , id rows.last(); //scustomerid = rows.getint(1); } catch (sqlexception e1) { e1.printstacktrace(); } object[] customer = {scustomerid,scustomername,saddress, scity,spostalcode,stelno,scontactperson }; // add row of values jtable dtablemodel.addrow(customer); } });
Comments
Post a Comment