Quantcast
Channel: MySQL Forums - Connector/J, JDBC and Java
Viewing all articles
Browse latest Browse all 884

Sql and java (2 replies)

$
0
0
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

Viewing all articles
Browse latest Browse all 884

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>