G'day,

I'm obtaining a set of data from the DB. I know that i must use an array, however i cannot for the life of me remember what type of array i need to use.

My data set contains String, Double and datetimes.
Should i use an arraylist ? or should i use a vector?

I would personally make an Object to represent the data coming out of the database, assuming that the data is related. If you want to stuff anything into an array, you can use an ArrayList (or Vector if you need it to be synchronized). If you don't need it to be synchronized use ArrayList. You can use something like ArrayList<Object> list = new ArrayList<Object>(); . I don't have a backround in databases so take that advice with a grain of salt, I'm avoiding being too specific lest I mislead you.

I agree. Define an oject that corresponds to a single row of the data table (with instance variables of the appropriate type for each field) the read the table into an ArrayList of those objects.

thanks guys,

i've gone with using vectors, however I'm still trying to figure them out cause i havent used them since 1st year uni lol.

thanks again

No problem. Assuming that you make an Object to put your data in like James and I suggested, the declaration I gave you above would just be changed to ArrayList<YourObject> list = new ArrayList<YourObject>(); . Same pattern applies for using Vector. The ArrayList class is actually pretty simple for someone who has studied Java before; you can check out the get() and add() methods at the Javadoc if you change your mind. Javadoc for Vector is here; you basically call yourVector.add(); to add an Object and .get(index) to get an Object at a particular index.

hey guys, thanks i really appreciate your help with this!!!

I've done java in a couple of my subjects while i was in uni but never had a real opportunity to delve into it proper before now.

Due to the nature of the program that i'm working on at the moment, i need to use multi-dimensional arrays(or vectors as the case may be).

I need my vector to look something like this:

v1 = {
{att1, att2, att3, att4, att5, att6},
{att1, att2, att3, att4, att5, att6},
{att1, att2, att3, att4, att5, att6}
};

I've written some code for doing so, though i'm unsure as to how i can iterate through the vectors to obtain the individual elements for each row which i have created. If you could please give me a few pointers on what i should be doing, i'd very much appreciate it !

The code which i have written is as follows;

Vector roomDetails;
Vector hotel;

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

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

            roomDetails = new Vector();
            hotel = new Vector();

            while(res.next()){
                roomDetails = new Vector();
                
                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();
        } finally {
             if (connect != null) {connect.close(); }
             if (con != null){ con.close(); }
             if (res!= null ){ res.close(); }
        }

        return hotel;
    }

What is wrong with creating a class on your own? Like I suggested in one of your previous posts?

Anyway, even though it is wrong, you can do this:

Vector hotel = ....;
for - loop {
  Vector rmDet = (Vector)hotel.get(i);
  // now iterate the rmDet Vector.
}

I have created a class of my own, however that is not the issue that i'm having.

The problem i am facing is that due to the nature of my database, and the tables therein. Each hotel has multiple roomTypes, and each roomType has one roomRate, discountRate, ect.

i require a multidimensional array/vector to hold the details of the roomtypes. 1 array to hold the details of a particular roomType, and 2nd to contain all of the array's pertaining to the roomType details.

This is my ORIGINAL class;

package Model;

import java.util.Vector;

public class hotel {

    // Attributes
    private int hotelID;
    private String hotelName;
    private int hotelRating;

    private Vector roomTypes;
    private Vector roomRates;
    private Vector roomDisc;


    // Member functions
    public void setHotelID( int hID){
        this.hotelID = hID;
    }

    public void setHotelName( String hName){
        this.hotelName = hName;
    }

    public void setHotelRating( int hRating){
        this.hotelRating = hRating;
    }

    public void setHotelRoomTypes(Vector rt){
        this.roomTypes = rt;
    }

    public void setHotelRoomRate(Vector rr){
        this.roomTypes = rr;
    }

    public void setHotelRoomDisc(Vector rd){
        this.roomTypes = rd;
    }
}

However, using individual vectors for each roomType detail doesn't work well, hence why i was attempting to use vectors. I have used the method which you have described in my other post, though i had some difficulties of my own in adapting the code which i had previously written (on my own), taking into account the fact that i need to use multidimensional vectors.

SO, will this work as intended???

roomDetails = new Vector();
hotel = new Vector();

while(res.next()){
    roomDetails = new Vector();
                
    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);
}

I need my vector to look something like this:

v1 = {
{att1, att2, att3, att4, att5, att6},
{att1, att2, att3, att4, att5, att6},
{att1, att2, att3, att4, att5, att6}
};

Then why don't you put the "att1, att2, att3, att4, att5, att6" is a class?

Also your hotel class is not quite right:

private Vector roomTypes;
private Vector roomRates;
private Vector roomDisc;

What is the point of having Vector with roomTypes and others? How is it going to be useful if you put all the roomTypes in one Vector? But if you say that each element of those Vectors are a room then try this:

private Vector rooms;

And have each room have those as attributes (roomType, ...)

I suppose that the database has many hotels. But your query is this:

SELECT * FROM hotelrooms WHERE hotelID = '"+hotelID+"'"

So assuming that your database is designed correct then each row of the hotelrooms table will have info of ONE room. That is what the query returns. So why don't you use a HotelRoom object as suggested. Even if: Each hotel has multiple roomTypes, and each roomType has one roomRate, discountRate, ect. as you said, each ROOM will have ONE: {roomType, roomRate, discountRate, ....}

If your database is structured in a way where in one table you have all the roomtypes with their description(roomRate, discountRate, roomTypeId), and another table for the hotelRooms with foreign keys to that table, then your query is not correct.

But with what given: "SELECT * FROM hotelrooms", Then each row of the query will have a given number of columns for one single room and each row would be an Object with attributes those columns.

Why would you need 1 Vector with all the roomtypes when all your query does is get each room, which has one rooom type?
And given that room type you have the details of each room.

Like I said if your database is structured the way I assumed, which means that I could be wrong with my assumption, then your query needs some more joins and where statements.

But in conclusion this:

roomDetails = new Vector();
                
    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);

Is bad programming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.