This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Breaking doXXX methods down into smaller ones Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Breaking doXXX methods down into smaller ones" Watch "Breaking doXXX methods down into smaller ones" New topic
Author

Breaking doXXX methods down into smaller ones

Gill Clover
Greenhorn

Joined: Aug 10, 2002
Posts: 28
Hi there,
I am quite new to servlets and am having problems structuring methods. To give you an example, I have a servlet that creates a session, connects to Oracle, executes a few SQL statements and outputs the results. I started out doing ALL of this in the doGet method, and it's very big and unmanageable!
So I'm trying to think of ways to best break it down into smaller methods. So far I've broken it doen into a method that handles checking for a session/creating one if none exists;
then I've got a method which opens a connection to Oracle (already using a helper class for this so the method in my servlet is very small) and executes the first SQL statement;
and finally I have a method which I'd like to do all the HTML output.
However, this doOutput method also contains some SQL queries because I couldn't see how to do a second query also in my database connection method unless I made a new connection, resultSet and statement, which seems wasteful and silly to do. Or I thought that I could make a new method for each SQL statement I want to execute but this also seemed a bit wasteful (although this is the best way I can think to do it). So now my subsequent queries are just being done in this doOutput method, which isn't ideal.
When I had everything in doGet, it was lots easier to reuse resultSet/connection/statement because everything was nice and sequential.
What I'd like to know is, how do you execute lots of SQL statements without making a new method for each one (or is that what you have to do)? I'm just finding it hard to totally separate what I output from internal stuff like getting stuff from the database.
I've tried looking on the Internet for a good example of this but can't find one - if someone could point me to one that uses lots of SQL queries or similar, and divides it all up neatly (or show me how with a bit of code), I'd be extremely grateful.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
I would recommend moving all business logic (SQL statements, etc) out of your servlet entirely and into a helper class. Your servlet would then be free to process web related logic such as session, request, and response management. You could then have your servlet forward the results of the business logic to a JSP containing your html and presentation logic.


Matthew Phillips
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
The greatest advantage to using a helper class (as far as I am concerned) is that you can test it offline. This lets you eliminate all sorts of confusing factors associated with the servlet interface and makes it easy to debug in stages.
Bill
[ November 20, 2002: Message edited by: William Brogden ]
Gill Clover
Greenhorn

Joined: Aug 10, 2002
Posts: 28
Hi there,
Thanks for your replies - OK, doing SQL in helper classes is fine but would you execute all your statements within the same method (and use a different resultSet for each one) or in different ones (and use the same resultSet if you were careful when you called each method)?
I can see advantages and disadvantages to each - does it just depend on personal preference or is there a standard practice?
Gillian Klee
[ November 20, 2002: Message edited by: Gillian Klee ]
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
Gillian,
You can create a class that gets your jdbc connection in the constructor. For example:
import java.sql.*;
public class MyData{
private Connection con;
private Statement stmt;
private ResultSet rs;
public MyData(String url, String user, String password){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url,
user,password);
stmt = con.createStatement();
}
catch(Exception e){
e.printStackTrace();
}
}
public ResultSet getMyData(String query){
rs = stmt.executeQuery(query);
//do some logic here
return rs;
}
public int UpdateMyDate(String updateQuery){
....
....
return stmt.executeUpdate(updateQuery);
}
//don't forget to call this in your servlet when you finish calling methods
public void close(){
try{
if(null != rs){
rs.close();
}
if(null != stmt){
stmt.close();
}
if(null != con){
con.close();
}
}
catch(SQLException sqlex){
//do something here
}
}
}
Is this what you are looking for? Hope this helps.


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Gill Clover
Greenhorn

Joined: Aug 10, 2002
Posts: 28
Originally posted by William Duncan:
Is this what you are looking for? Hope this helps.

William, thanks for your input. I've already got a helper class that I call to do the JDBC connection...
But regarding the rest of your code: you've got two methods doing SQL things, getMyData and updateMyDate... so what you're saying is that you would have separate methods for each time you execute an SQL query/update etc?
Gillian
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Seems to me that separate methods would be easier to debug, catch exceptions, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Breaking doXXX methods down into smaller ones
 
Similar Threads
best programming practises of JDBC
Java application Microsoft SQL server
connection to mssql server database
DB connection in a servlet question
java.sql.SQLException: Closed Connection