chicago1985
Technical User
I have a repeated resultset object that I use alot to execute a statement that fetches max id from a table.
I was wondering if I can put it in a method and call the method each time I need the max id?
The repeated part is:
Resultset rs = statement.executeQuery("select max(id) from TableMain");
rs.next();
Here is an example of what I am doing now:
I need help on my attempt below because I am not sure how to do it:
Call it like this:
I was wondering if I can put it in a method and call the method each time I need the max id?
The repeated part is:
Resultset rs = statement.executeQuery("select max(id) from TableMain");
rs.next();
Here is an example of what I am doing now:
Code:
Statement statement = connection.createStatement();
if(condition here..)
{
Resultset rs = statement.executeQuery("select max(id) from TableMain");
rs.next();
a = rs.getInt(1);
//my insert sql is here to insert into another table the value of the max id....
//another call to get the max id:
if(another condition here...)
{
Resultset rs = statement.executeQuery("select max(id) from TableMain");
rs.next();
c = rs.getInt(1);
//my insert sql is here to insert into another table the value of the max id....
I need help on my attempt below because I am not sure how to do it:
Code:
public ResultSet getMaxId()
{
Resultset rs = statement.executeQuery("select max(id) from TableMain");
rs.next();
return rs;
}
Call it like this:
Code:
if(any condition here..)
{
getMaxId();
f = rs.getInt(1);
///my insert sql is here to insert into another table the value of the max id....