• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Breaking doXXX methods down into smaller ones

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gill Clover
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to me that separate methods would be easier to debug, catch exceptions, etc.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic