I'm sitting my CMA next week, and am rather embarrassed that i have to ask this.
i have some java code, that i'm looking to clean up. and in it is my SQL queries. I've tried running joins on my tables but none quite get the effect that i've achieved with my following code.
If you could please help with the MySQL it would be much appreciated.
try {
Connection con = new database().getConnection();
Statement stmt = con.createStatement();
/* SELECT ALL PACKAGE DETAILS */
String packageDetails = "SELECT * FROM packages";
ResultSet res = stmt.executeQuery(packageDetails);
while(res.next()) {
count++;
}
/* CREATE NEW attractionPackage array */
packageList = new attractionPackage[count];
res.beforeFirst();
while (res.next()) {
packageList[i] = new attractionPackage();
packageList[i].setPackageID(res.getInt("pacID"));
packageList[i].setPackagePrice(res.getDouble("packagePrice"));
try{
Connection connect = new database().getConnection();
Statement statement = connect.createStatement();
/* SELECT ALL DETAILS FROM attractionpackages WHICH CORRESPOND TO THE CURRENT pacID */
String query = "SELECT * FROM attractionpackages WHERE pacID = '"+packageList[i].getPackageID()+"' ORDER BY pacID ASC";
ResultSet results = statement.executeQuery(query);
int c = 0;
int x = 0;
while(results.next()){
c ++;
}
String[] attractions = new String[c];
results.beforeFirst();
while(results.next()){
attractions[x] = results.getString("attID");
x++;
}
packageList[i].setAttractions(attractions);
}catch (SQLException e) {
e.printStackTrace();
}
i++;
}
} catch (SQLException e) {
e.printStackTrace();
}
My table structure is as follows;
CREATE TABLE IF NOT EXISTS `packages` (
`pacID` int(11) NOT NULL AUTO_INCREMENT,
`packagePrice` double NOT NULL,
PRIMARY KEY (`pacID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
CREATE TABLE IF NOT EXISTS `attractionpackages` (
`attID` varchar(255) NOT NULL,
`pacID` int(11) NOT NULL,
PRIMARY KEY (`attID`,`pacID`),
FULLTEXT KEY `attID` (`attID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;