G'day,
I've got rather a simple problem to deal with though i'm personally having some trouble.
I've recently fixed up some of my database queries so that i use a view to get the data that i need.
My view gets the following info:
- Attraction Name
- Package ID
- Package Price
Each attraction name has an Package ID, and a Package price. So my table looks like this
Zoo 1 55.00
Circus 1 55.00
Safari 2 88.99
Temple 2 88.99
Park 2 88.99
... And so on.
I need to be able to store the attraction names in either a vector or an array/arrayList
while having only one instance of the Package ID and Package Price being stored.
My Code is as Follows:
public attractionPackage[] getPakcage(){
int numPack = 0; // Number of packages
int i = 0; // Counter for packageList
try{
Connection connect = new database().getConnection();
Statement statement = connect.createStatement();
// Query Selects Attraction Name, Package ID and Package Price
String query = "SELECT * FROM attpack_view";
ResultSet res = statement.executeQuery(query);
// Query gets number of DISTINCT Package ID entries
String numPackages = "SELECT DISTINCT COUNT( pacID ) AS num FROM packages";
ResultSet result = statement.executeQuery(numPackages);
// Set number of packages
while(result.next()){
numPack = result.getInt("num");
}
// Initialize packageList as attractionPackage[]
packageList = new attractionPackage[numPack];
/**************************
* THIS IS WHERE I NEED HELP
***************************/
int previous_id = -1;
while (res.next()) {
int current_id = res.getInt("pacID");
if (current_id != previous_id) {
packageList[i].setPackageID(res.getInt("pacID"));
packageList[i].setPackagePrice(res.getDouble("pacID"));
previous_id = current_id;
}else {
// process the next (same) pac_id with a different att_id
}
} // End WHILE
// Catch exceptions thrown by SQL
}catch(SQLException e){
// OUTPUT Errors
e.printStackTrace();
}
return packageList;
}
I'd appreciate any help you could give. I understand that my code isnt the cleanest or most efficient, but for the most part it works.
Thx
TC