繁体   English   中英

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

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

所以我有一个返回多行和多列信息的 SQL 查询,但我无法在 textArea 中显示多行。 下面的代码正确显示了第一行信息,但我不确定如何让它也显示其他 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");

这是 SQL 命令返回的内容 ( https://imgur.com/a/odkoCpr )

文本区域仅显示第一行( https://imgur.com/a/mNHf9nZ

您在同一 while 循环中使用 carid2,这意味着 cursor 仍处于记录号 1。此代码不正确。 您可以执行一些操作,例如在字符串生成器中设置所有值,最后在其附加更多记录。

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