Put SQL statement into a method

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....
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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();

Ofcourse you can make a method:

public int getMaxId(Statement stmt) {
....
}

Arne

PS: In 99.9% of cases where people want to do a SELECT MAX(id) then
they are much better of doing something else.
 
L

Lew

I know I have mentioned repeatedly in answer to this question as you have
reposted it, and others have too, that you absolutely must check the return
value of rs.next().

If you don't value our advice, why do you request it?
 
R

ram00540

I know I have mentioned repeatedly in answer to this question as you have
reposted it, and others have too, that you absolutely must check the return
value of rs.next().

If you don't value our advice, why do you request it?

hi you can create the function is as below.
public static int getMax(String column, String table) {
PreparedStatement st = null;
ResultSet rs = null;
Connection con = null;

int maxid = 0;
try {
con = DatabaseUtil.getConnection();
String query = "select max(" + column + ") from " + table + ";";
st = con.prepareStatement(query);
rs = st.executeQuery();
while (rs != null && rs.next()) {
maxid = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();

if (st != null)
st.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return maxid;
}

}
and call every time you have required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top