T
teser3
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:
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.
My attempts keep giving me zero for max id or I dont fetch anything.
Please advise.
public ResultSet getMaxId()
{
Resultset rs = statement.executeQuery("select max(id) from
TableMain");
rs.next();
return rs;
}
Call it like this:
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....
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:
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.
My attempts keep giving me zero for max id or I dont fetch anything.
Please advise.
public ResultSet getMaxId()
{
Resultset rs = statement.executeQuery("select max(id) from
TableMain");
rs.next();
return rs;
}
Call it like this:
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....