java中如何从数据库中检索多行
我需要在JTable的行中插入不同的结果。我使用purchaseID(pID)搜索我的记录,它将为特定的purchaseID找到2个结果。问题是当我将它插入到行中时,它只是复制了第一个的结果。例如:P0001有两个购买明细,当我将其插入行中时,我有两个相同的行。有没有办法让我一行一行地插入 另外,我从早上就开始编码了,大脑功能不正常 用户界面: 域:java中如何从数据库中检索多行,java,database,netbeans,jtable,Java,Database,Netbeans,Jtable,我需要在JTable的行中插入不同的结果。我使用purchaseID(pID)搜索我的记录,它将为特定的purchaseID找到2个结果。问题是当我将它插入到行中时,它只是复制了第一个的结果。例如:P0001有两个购买明细,当我将其插入行中时,我有两个相同的行。有没有办法让我一行一行地插入 另外,我从早上就开始编码了,大脑功能不正常 用户界面: 域: public Purchase(String purchaseDetailsID,String purchaseID,String stockID
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(){
return purchaseID;
}
public String getPurchaseDetailsID(){
return purchaseDetailsID ;
}
public double getPrice(){
return price;
}
public String getStockID(){
return stockID;
}
public int getQuantity(){
return 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;
}
控制:
public Purchase selectRecord(String pID){
return purcDA.getRecord(pID);
}
编辑:将Purchase设置为ArrayList以接受该列表,我会收到此异常
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at ui.MainPurchasingFrame$ListenerClass.actionPerformed(MainPurchasingFrame.java:183)
代码:
ArrayList purchase=purcControl.selectRecord(pID);
如果(购买!=null){
字符串pdCountStr=purchase.get(0.getPurchaseDetailsID();
字符串pdCountStr2=pdCountStr.子字符串(2,5);
int pdCount=Integer.parseInt(pdCountStr2);
系统输出打印(pdCount);
对于(int g=0;g如果要从查询中返回多个对象,则返回类型应为该类型的数组或集合
因此,与其定义
public Purchase getRecord(String pID){…}
您应该将DA方法定义为:
public List<Purchase> getRecords(String pID) {…}
然后,在完成并关闭对账单后,您应该返回foundPurchases:
return foundPurchases;
当然,调用代码应该期望接收购买的列表
,而不是购买,然后迭代并显示它
现在,关于你编辑的部分。你做了一些不正确的事情
首先,您不必查看采购记录中有多少条记录。如果没有,列表将为空。如果有1条,它的大小将为1,如果有两条,它的大小将为2
第二,您实际上不需要知道有多少元素,因为您可以在一个简单的for循环中使用迭代器遍历列表:
ArrayList<Purchase> purchaseList = purcControl.selectRecord(pID);
for ( Purchase purchase : purchaseList ) {
…
}
使用while
循环而不是if
语句?并始终关闭资源。while(rs.next())?它只会替换这个东西。嗯,当我尝试获取第二个时,我会得到IndexOutOfBoundsException。它说1时超出范围。它在哪里这么说?在我写的其中一行中?你可以将新代码添加到原始问题中,标题为“Edit changed the code to loop,现在这是我的新代码,我得到了这个异常”。别忘了标记发生异常的行。我已完成编辑,请查看它。尝试向右滚动以查看错误标记。编辑了我的答案以涵盖您的新问题。因此我可以直接将其称为tableModel.addRow(新对象[]{purchaseList})?
public List<Purchase> getRecords(String pID) {…}
List<Purchase> foundPurchases = new ArrayList<>();
while ( rs.next() ) {
purchase = new Purchase(pID, rs.getString("purchaseID"),rs.getString("stockID"),rs.getDouble("stockprice"),rs.getInt("orderqty"));
foundPurchases.add( purchase );
}
return foundPurchases;
ArrayList<Purchase> purchaseList = purcControl.selectRecord(pID);
for ( Purchase purchase : purchaseList ) {
…
}
if ( purchasedList.isEmpty() ) {
// Display error dialog
} else {
// Do the for loop to fill the rows
}