0

G'day,

I have the following java code to get all of my data from the DB, and is being passed into a vector in my jsp page. My question is, how do i iterate through the vector in jsp?

Vector roomDetails;
Vector hotel;

...

    public Vector getHotelRooms(int hotelID){
        int count = 0;
        try{
            Connection connect = new database().getConnection();
            Statement con = connect.createStatement();

            String sql = "SELECT * FROM hotelrooms WHERE hotelID = '"+hotelID+"'";
            ResultSet res = con.executeQuery(sql);

            while(res.next()){
                count ++;
            }

            res.beforeFirst();
            roomDetails = new Vector();
            hotel = new Vector();

            while(res.next()){
                roomDetails.add(res.getString("roomType"));
                roomDetails.add(res.getDouble("roomRate"));
                roomDetails.add(res.getDouble("discountRate"));
                roomDetails.add(res.getString("discountValidityStart"));
                roomDetails.add(res.getDouble("discountValidityEnd"));

                hotel.add(roomDetails);

            } 

        }catch(SQLException exc){
            exc.printStackTrace();
        }

        return hotel;

    }
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by javaAddict
1

First of all learn how to read data from the database and proper OOP.
When you copy code you must understand what it does. Not use it blindly:

while(res.next()){
                count ++;
            }

            res.beforeFirst();
..

Where in your code you use the 'count' variable and what is its purpose? Do you understand what that code does? If yes, then why do you use it, since you are already using Vector. When you have vector you don't need the total count.

Also this doesn't do what you think:

while(res.next()){
                roomDetails.add(res.getString("roomType"));
                roomDetails.add(res.getDouble("roomRate"));
                roomDetails.add(res.getDouble("discountRate"));
                roomDetails.add(res.getString("discountValidityStart"));
                roomDetails.add(res.getDouble("discountValidityEnd"));

                hotel.add(roomDetails);

            }

You add data in the roomDetails and then add it to the hotel vector. But at the next loop you add the next row to the same roomDetails instance!!!

After the first loop the roomDetails has: {roomType,roomRate,...,discountValidityEnd}.

After the second loop roomDetails has:
{roomType,roomRate,...,discountValidityEnd, roomType,roomRate,...,discountValidityEnd}.

After the third loop roomDetails has:
{roomType,roomRate,...,discountValidityEnd, roomType,roomRate,...,discountValidityEnd,
roomType,roomRate,...,discountValidityEnd}.

Because you add them at the same instance.

And then you add the same instance to the hotel Vector. That would mean that all the elements of the hotel Vector are the same roomDetails instance, meaning that all the elements of the hotel Vector have something like this:
hotel = {
{roomType,roomRate,...,discountValidityEnd, roomType,roomRate,...,discountValidityEnd,
roomType,roomRate,...,discountValidityEnd,.......},

{roomType,roomRate,...,discountValidityEnd, roomType,roomRate,...,discountValidityEnd,
roomType,roomRate,...,discountValidityEnd,.......},


{roomType,roomRate,...,discountValidityEnd, roomType,roomRate,...,discountValidityEnd,
roomType,roomRate,...,discountValidityEnd,.......},
....
}


The answer:
Declare a class: HotelRoom with attributes: roomType, roomRate, ....
Inside the loop, you read the next row. So you would need a new HotelRoom to put into the Vector:

hotel = new Vector();

while (rs.next()) {
  HotelRoom room = new HotelRoom();
  room.setRoomType(res.getString("roomType"));
  ....

  hotel.add(roomDetails);
}

With each loop you add a new HotelRoom to the Vector, because each row is a different Room.

Also you must close the ResultSet, Connection and Statement. So you should put them in a finally block:

Connection connect = null;
try {
    connect = new database().getConnection();
} catch (Exception e) {

} finally {
  if (connect!=null) connect.close();
}

Inside the jsp after you have the hotel Vector:

<%
Vector hotel = ....
...
%>
..
..
<%
for (int i=0;i<hotel.size();i++) {
  HotelRoom room = (HotelRoom)hotel.get(i);
%>
  <%= room.getRoomType()%>, .....
<%
}
%>

You can put a combination of <tr>, <td> inside the for-loop to display the data in a <table>, but it is best before continuing to read some tutorial on the matter. There is a good one at the top of this forum with title database connectivity (MVC)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.