File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Problem in Seperating Core Servlet to Database Query Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problem in Seperating Core Servlet to Database Query Java" Watch "Problem in Seperating Core Servlet to Database Query Java" New topic

Problem in Seperating Core Servlet to Database Query Java

Ken Shamrock
Ranch Hand

Joined: Jan 23, 2002
Posts: 139
I want to seperate database query function(like get a resultset) from my servlets, so I will need to make a contains following code(to return a ResultSet to my servlet):
private ResultSet userquery(String driverName,String connectionURL,String query, out,String a,String uri)
Connection con = null; // JDBC Connection
Statement stmt = null; // JDBC Statement
ResultSet rs = null; // JDBC ResultSet

try {
con = DriverManager.getConnection(connectionURL);

stmt = con.createStatement();
// Execute the query
rs = stmt.executeQuery(query);
return rs;
catch (Exception ex) {
// Send the error back to the client
rc = false;
finally {
try {
// Always close properly
if (rs != null) {
if (stmt != null) {
if (con != null) {
catch (Exception ex) {
// Ignore any errors here
My servlet will create this database object and call this method eg:
Database database=new database();
userquery(driverName,connectionURL,"Select * from table",...)
But it seems conflicts and can't return a resultset to my servlet as it will close resultset (rs.close) before it leave. How can I rearrage and make it works? Thanks very mucj

Thanks everyone who helped me
Michael Yuan
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
If you do not want database code in your servlet, you probablly do not want the servlet to process resultset. In the "database" object, you should parse the resultset into a Vector and return the "business information" back to the servlet.
Or, you can use JavaBeans to replace the "database" object and provide meaningful data access API to your database tables.

Seam Framework:
I agree. Here's the link:
subject: Problem in Seperating Core Servlet to Database Query Java
It's not a secret anymore!