This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes SQL statment into a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQL statment into a method" Watch "SQL statment into a method" New topic
Author

SQL statment into a method

Ed Carrington
Ranch Hand

Joined: Oct 11, 2007
Posts: 77
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? All my attempts dont fetch the last max id:



Call it like this:


[ October 11, 2007: Message edited by: Ed Carrington ]
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi,

"All my attempts dont fetch the last max id"

What DO you get? SQLException maybe?

Herman
Vallidevi Appana
Greenhorn

Joined: Jun 30, 2006
Posts: 25
You can try return id from the method instead of result set.



Call this method by passing statment object.

Hope this will solve your problem.
Ed Carrington
Ranch Hand

Joined: Oct 11, 2007
Posts: 77
Thanks, that worked great for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SQL statment into a method
 
Similar Threads
prepareStatement returns 0?
Tomcat 5 _ Mysql 5
Select max id and calling method from another class
prepareStatement returns 0?
Update inside Select statement