简体   繁体   English

如何显示在 Java 中返回多行的 SQL 查询

[英]How to display a SQL Query that returns multiple rows in Java

So I have a SQL Query that returns multiple rows and columns of information but I am having trouble displaying more than a single row in a textArea.所以我有一个返回多行和多列信息的 SQL 查询,但我无法在 textArea 中显示多行。 The code below correctly displays the first row of information but I'm not sure how I could get it to also display the other 4 rows of information.下面的代码正确显示了第一行信息,但我不确定如何让它也显示其他 4 行信息。


 String sql4 = "SELECT * FROM CarDescription JOIN LotCarList  ON CarDescription.CarId = LotCarList.CarId JOIN CarLots ON LotCarList.LotId = CarLots.LotId JOIN MakeModel ON CarDescription.MId = MakeModel.MId";
                 
                 PreparedStatement pstmt = con.prepareStatement(sql4);
                 
                 
                 
                 
                 ResultSet rs = pstmt.executeQuery();
                 
                 while(rs.next()) {
                     
                     int carid = rs.getInt(n);
                     n++;
                     int mid = rs.getInt(n);
                     n++;
                     String color = rs.getString(n);
                     n++;
                     int mileage = rs.getInt(n);
                     n++;
                     int Price = rs.getInt(n);
                     n++;
                     int listid = rs.getInt(n);
                     n++;
                     int lotid = rs.getInt(n);
                     n++;
                     int carid2 = rs.getInt(n);
                     n++;
                     int lotid2 = rs.getInt(n);
                     n++;
                     String lotname = rs.getString(n);
                     n++;
                     String lotadd = rs.getString(n);
                     n++;
                     int mid2 = rs.getInt(n);
                     n++;
                     String make = rs.getString(n);
                     n++;
                     String model = rs.getString(n);
                     
                     
                     
                     
textArea.append(" CarID: "+carid + "\n " + "MakeID: " + mid + "\n " + "Color "+ color + "\n " + "mileage " + mileage + "\n " + "Price " + Price + "\n " + "ListId: "+ listid + "\n" + "LotId: "+ lotid + "\n" + "CarId2: "+ carid2 + "\n" + "lotid2: "+ lotid2 + "\n" + "Lotname: "+ lotname + "\n" + "Lotadd: "+ lotadd + "\n" + "Mid2: "+ mid2 + "\n" + "Make: "+ make + "\n" + "Model: "+ model + "\n");

Here is what the SQL command returns ( https://imgur.com/a/odkoCpr )这是 SQL 命令返回的内容 ( https://imgur.com/a/odkoCpr )

The textarea is showing only the first row ( https://imgur.com/a/mNHf9nZ )文本区域仅显示第一行( https://imgur.com/a/mNHf9nZ

You are using carid2 in the same while loop which means the cursor is still at record number 1. This code is not correct.您在同一 while 循环中使用 carid2,这意味着 cursor 仍处于记录号 1。此代码不正确。 You can do something like setting all values in a string builder and finally appending it with more records.您可以执行一些操作,例如在字符串生成器中设置所有值,最后在其附加更多记录。

StringBuilder sb = new StringBuilder();
while(rs.next()) {
                     
                     int carid = rs.getInt(n);
                     n++;
                     int mid = rs.getInt(n);
                     n++;
                     String color = rs.getString(n);
                     n++;
                     int mileage = rs.getInt(n);
                     n++;
                     int Price = rs.getInt(n);
                     n++;
                     int listid = rs.getInt(n);
                     n++;
                     int lotid = rs.getInt(n);
                     n++;
sb.append("CarID : "+carid);
sb.append("\n");
..
...
...
sb.append("Lot ID:"+lotid);
sb.append("\n");
n=0;
}

textArea.append(sb.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM