How to retrieve multiple row from database in java -
i need insert different result row of jtable. search record using purchaseid pid, find 2 result particular purchaseid. problem when insert row, duplicate result of first one. eg : p0001 have 2 purchase details , when insert row, have 2 same row. there way me insert row 1 1 ?
ps. i've been coding since morning, brain not functioning well.
ui:
else if(e.getsource() == jbtretrieve) { string pid = jtf3.gettext(); purchase purchase = purccontrol.selectrecord(pid); if (purchase != null) { string pdcountstr = purchase.getpurchasedetailsid(); string pdcountstr2 = pdcountstr.substring(2,5); int pdcount = integer.parseint(pdcountstr2); for(int g = 0; g<pdcount;g++){ //g =1 tablemodel.addrow(new object[]{ purchase.getpurchasedetailsid(),purchase.getstockid(),purchase.getprice(),purchase.getquantity()}); }}
da :
public purchase getrecord(string pid){ string querystr = "select pd.purchasedetailid,pd.purchaseid,pd.stockid,pd.orderqty,s.stockprice "+tablename+" pd, stocks s pd.stockid = s.stockid , purchaseid = ?"; purchase purchase = null; system.out.println("asd"); try{ stmt = conn.preparestatement(querystr); stmt.setstring(1,pid); resultset rs = stmt.executequery(); if(rs.next()){ purchase = new purchase(pid, rs.getstring("purchaseid"),rs.getstring("stockid"),rs.getdouble("stockprice"),rs.getint("orderqty")); } } grab (sqlexception ex){ joptionpane.showmessagedialog(null,ex.getmessage(),"error",joptionpane.error_message); } homecoming purchase; }
domain:
public purchase(string purchasedetailsid,string purchaseid,string stockid,double price,int quantity) { this.purchasedetailsid = purchasedetailsid; this.purchaseid = purchaseid; this.stockid = stockid; this.price = price; this.quantity = quantity; } public void setpurchaseid(string u){ this.purchaseid = u; } public string getpurchaseid(){ homecoming purchaseid; } public string getpurchasedetailsid(){ homecoming purchasedetailsid ; } public double getprice(){ homecoming price; } public string getstockid(){ homecoming stockid; } public int getquantity(){ homecoming quantity; } public void setpurchasedetailsid(string r){ this.purchasedetailsid = r ; } public void setprice(double p){ this.price = p; } public void setstockid(string s){ this.stockid = s; } public void setquantity(int q){ this.quantity = q; }
control :
public purchase selectrecord(string pid){ homecoming purcda.getrecord(pid); }
edit: set purchase arraylist<> take list , exception.
exception in thread "awt-eventqueue-0" java.lang.indexoutofboundsexception: index: 1, size: 1 @ java.util.arraylist.rangecheck(arraylist.java:635) @ java.util.arraylist.rangecheck(arraylist.java:635) @ java.util.arraylist.get(arraylist.java:411) @ ui.mainpurchasingframe$listenerclass.actionperformed(mainpurchasingframe.java:183)
code:
arraylist<purchase> purchase = purccontrol.selectrecord(pid); if (purchase != null) { string pdcountstr = purchase.get(0).getpurchasedetailsid(); string pdcountstr2 = pdcountstr.substring(2,5); int pdcount = integer.parseint(pdcountstr2); system.out.print(pdcount); for(int g = 0; g<pdcount;g++){ tablemodel.addrow(new object[]{ purchase.get(g).getpurchasedetailsid(),purchase.get(g).getstockid(),purchase.get(g).getprice(),purchase.get(g).getquantity()}); //this exception occurs. }
if want homecoming multiple objects query, homecoming type should either array or collection of type.
so instead of defining
public purchase getrecord(string pid){…}
you should define da method as:
public list<purchase> getrecords(string pid) {…}
inside method, define list object such as:
list<purchase> foundpurchases = new arraylist<>();
and after issuing query , getting result set, utilize while loop fill it:
while ( rs.next() ) { purchase = new purchase(pid, rs.getstring("purchaseid"),rs.getstring("stockid"),rs.getdouble("stockprice"),rs.getint("orderqty")); foundpurchases.add( purchase ); }
then, after finish , close statement, should homecoming foundpurchases:
return foundpurchases;
the calling code should, of course, expect receive list
of purchases rather purchase, , iterate , display it.
now regarding edited part. doing several things improperly.
first of all, don't have go within purchase records see how many records there were. if there none, list empty. if there 1, it's size going 1, , if there two, size going two.
second, don't need know how many elements there are, because can traverse list using iterator, in simple loop:
arraylist<purchase> purchaselist = purccontrol.selectrecord(pid); ( purchase purchase : purchaselist ) { … }
you don't have utilize get
. have current individual purchase in variable purchase
(note changed name of original variable shows it's list , not purchase), , can utilize directly.
note don't need check nulls. way i've written it, da method returns reference list, if it's empty list. , loop adds actual new purchase(…)
list list's items never null.
one thing, though: way error looks, seems have 1 row when details id says there should two. check query straight database sure there 2 records.
if want display error when nil found, should check if
statement if list empty. means no records have been returned @ all.
if ( purchasedlist.isempty() ) { // display error dialog } else { // loop fill rows }
java database netbeans jtable
No comments:
Post a Comment