Sql and java
Posted by: mohd sherrez
Date: July 20, 2014 04:45AM

Hi guys i need some help i have trying to modify the following dbquery to get id
public static ArrayList<Expenses> expensesByCategory(String searchPhrase){
// declare local variables
ArrayList<Expenses> expensesList = new ArrayList<Expenses>();

ResultSet rs = null;
DBController db = new DBController();
String dbQuery;

//Step 1 -connect to database
db.getConnection();

//step 2 - declare the SQL statement
dbQuery = "SELECT * FROM EXPENSES WHERE CATEGORY='" + searchPhrase +"'";


// step 3 - using DBController, use updateRequest method
rs = db.readRequest(dbQuery);
try{
while (rs.next()){
int eID = rs.getInt("id");
String spend = rs.getString("dateSpend");
String cat = rs.getString("category");
double amt = rs.getDouble("amount");
String cont = rs.getString("content");

Expenses ex = new Expenses(eID, spend, cat, amt, cont);
expensesList.add(ex);
}
}
catch (Exception e) {
e.printStackTrace();
}
// step 4 - close connection
db.terminate();
return expensesList;
}
i have tried to modify it to work with and int here is what i got
public static ArrayList<AthleteEntity> athleteByID(int searchPhrase){
// declare local variables
ArrayList<AthleteEntity> athleteList = new ArrayList<AthleteEntity>();

ResultSet rs = null;
DBController db = new DBController();
String dbQuery;

//Step 1 -connect to database
db.getConnection();

//step 2 - declare the SQL statement
dbQuery = "SELECT * FROM ATHLETEINFORMATION WHERE ID ==>1 AND ID <= 100000";


// step 3 - using DBController, use updateRequest method
rs = db.readRequest(dbQuery);
try{
while (rs.next()){
int eID = rs.getInt("id");
String Name = rs.getString("Name");
String Dob = rs.getString("Dob");
String idnumber = rs.getString("idnumber");
String contact= rs.getString("contact");
String gender= rs.getString("gender");
String sportplayed= rs.getString("sportplayed");


AthleteEntity at = new AthleteEntity (eID, Name, Dob, idnumber, contact,gender,sportplayed);
athleteList.add(at);
}
}
catch (Exception e) {
e.printStackTrace();
}
// step 4 - close connection
db.terminate();
return athleteList;
}
}
i want to print in out in this tbllist ArrayList<AthleteEntity>result=AthleteEntity.athleteByID(searchPhrase);
i cant seem to make it work as i want to print a range of id from 1
any hellp appreciated thanks

Options: ReplyQuote


Subject
Written By
Posted
Sql and java
July 20, 2014 04:45AM
July 21, 2014 11:27AM
July 21, 2014 06:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.