java - Table model unable to retrieve the data from database -
i coded tablemodel
table. successful add table column name unable retrieve data database. if 1 have questions post comment.
error
unable retrieve data database.
code
import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.sql.*; import java.util.vector; import java.util.logging.level; import java.util.logging.logger; import javax.swing.*; import javax.swing.table.defaulttablemodel; public class customer_bills { public static void main(string[] args) { customer_bills testtable = new customer_bills(); } public customer_bills() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new customer_bills.bill()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class bill extends jpanel implements actionlistener { jtextfield textfieldid; jlabel l1; jlabel l2; jbutton b1,b2,b3; jtextfield sun,sunr,sat,satr,oth,othr; float sum1,totall; resultset rs1 = null; defaulttablemodel model = new defaulttablemodel(); jtable table = new jtable(model); private int rows; public bill() { setlayout(new borderlayout()); jpanel fields = new jpanel(); textfieldid = new jtextfield(10); l1 = new jlabel("new customer entry :-"); l2 = new jlabel("customer id"); b1 = new jbutton("ok"); b2 = new jbutton("calculate"); b3 = new jbutton("print"); fields.add(l2); fields.add(textfieldid); fields.add(b1); fields.add(b2); fields.add(b3); add(fields, borderlayout.north); b1.addactionlistener(this); b2.addactionlistener(this); b3.addactionlistener(this); // don't forget add table. add(new jscrollpane(new jtable(model))); } @override public void actionperformed(actionevent e) { system.out.println("you clicked button"); connection con; if (e.getsource() == b1) { preparedstatement ps = null; statement stmt = null; try { class.forname("sun.jdbc.odbc.jdbcodbcdriver"); con = drivermanager.getconnection("jdbc:odbc:dalvi"); ps = con.preparestatement("select * customer_details customer_id = ?"); ps.setstring(1, textfieldid.gettext()); rs1 = ps.executequery(); model.addcolumn("paper name"); model.addcolumn("monday"); model.addcolumn("tuesday"); model.addcolumn("wednesday"); model.addcolumn("thrsday"); model.addcolumn("friday"); model.addcolumn("saturday"); model.addcolumn("sunday"); model.addcolumn("magzine name"); model.addcolumn("quantity"); model.addcolumn("total"); while (rs1.next()) { model.addrow(new object[] {rs1.getstring(1),rs1.getstring(2),rs1.getstring(3),rs1.getstring(4) ,rs1.getstring(5),rs1.getstring(6),rs1.getstring(7),rs1.getstring(8), rs1.getstring(9), rs1.getstring(10),rs1.getstring(11)}); } vector data = model.getdatavector(); joptionpane.showmessagedialog(null,"you enter entry"); } catch (sqlexception s) { system.out.println("sql code not execute."); joptionpane.showmessagedialog(null,"please enter detail correctly"); } catch (exception exp) { joptionpane.showmessagedialog(this,"failed perform query:"+exp.getmessage()); } { try { ps.close(); } catch (exception ex) { } } if (e.getsource() == b2) { int rowcount = table.getrowcount(); for(int =1; i<rowcount;i++) { object valuea = table.getvalueat( rowcount, 5 ); object valueb = table.getvalueat( rowcount, 6 ); object valuec = table.getvalueat( rowcount, 7 ); object valued = table.getvalueat( rowcount, 8 ); object valuee = table.getvalueat( rowcount, 9 ); object valuef = table.getvalueat( rowcount, 10 ); sum1=(float)((float) (double.parsedouble(string.valueof(valuea))*double.parsedouble( string.valueof( valueb )))+ (double.parsedouble(string.valueof(valuec))*double.parsedouble( string.valueof( valued )))+ (double.parsedouble(string.valueof(valuee))*double.parsedouble( string.valueof( valuef )))); // table.setvalueat(sum1, rowcount,11); string query = "insert customer values(table.getmodel().setvalueat(sum1, rowcount,11)"; try { stmt.executeupdate(query); } catch (sqlexception ex) { logger.getlogger(customer_bills.class.getname()).log(level.severe, null, ex); }} } } }}}
Comments
Post a Comment