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.