Skip to content
Advertisement

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. 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.

 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)

The textarea is showing only the first row (https://imgur.com/a/mNHf9nZ)

Advertisement

Answer

You are using carid2 in the same while loop which means the cursor is still at record number 1. This code is not correct. 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());
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement