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 Collection of Objects in JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Collection of Objects in JDBC" Watch "Collection of Objects in JDBC" New topic

Collection of Objects in JDBC

Abhin Balur

Joined: Feb 18, 2008
Posts: 21
Hi folks,

I am having a small problem in my implementation of Data Access Object Pattern. I have created a collection of objects in displayTable() method of the AccessTableDAO as shown below. I am calling this method from a servlet to access this Collection of objects and print all the details. However the problem is that i havent been able to. Wat i get is a null Collection(by checkin size). The code of both the servlet and the class creating the Collection is shown below. The other classes are not shown . They have been rechecked many times and can assumed to be logically correct and error free.
I feel either there is some problem with the Connection code or something wrong in the try catch block.

import java.sql.*;
import java.util.*;

public class AccessTableDAO implements TableDAO {

public AccessTableDAO()
// initialization


public Collection displayTable() {

Connection con=null;
Statement stmt = null;
ResultSet rs = null;

Collection coll= new ArrayList(); //for objects


con = DriverManager.getConnection("jdbcdbc:db2");

stmt = con.createStatement();

rs = stmt.executeQuery("SELECT * FROM table");

while (
Table tab = new Table(rs.getInt(1), rs.getString(2),rs.getInt(3),rs.getInt(4));



}catch(SQLException e) {
System.out.println( e);}
catch (ClassNotFoundException e) { System.out.println(e);}

return coll;


} (Servlet):-

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class Client extends HttpServlet


public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException

PrintWriter out = res.getWriter();



// create the required DAO Factory
DAOFactory accessFactory =

// Create a DAO
TableDAO tabDAO = accessFactory.getTableDAO();

Collection tablelist=new ArrayList();
tablelist=tabDAO.displayTable(); //retrieve the collection

if(tablelist.size()==0) //here is da problem(this is always true)



Iterator it= tablelist.iterator();


Table element=(Table);




Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Does your query return any results when you run it in a sql client? And why are you using the JDBC-ODBC bridge? DB2 has its own type 4 driver doesn't it?
[ April 23, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I guess class Table is a standard part of database code so what I see is ( first ) I think we have new tablesist = new ArrayList(); assigned to by the next statement which returns I do not know what but syntax normally would be List tablelist = tabDAO.method() or something. For one thing, I would think a List should go to a list. For another the return type of tabDAO.displayTable() should be consulted. Another thing is that new ArrayList() is (probably) not needed if the code immediately assigns the return from displayTable()

Second,{  ;)  }, we need to use correct spelling. I can read this and it is asking a question but we waste time while some sherrif who has better things to do tells us not to use variant spelling.

Correct the above before making any changes to Connection code or the try / catch. We have plenty of masters in conventional database stuff.

[Paul, I was guessing. You know more about this so just delete this if non-productive. Nick]
[ April 23, 2008: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
I agree. Here's the link:
subject: Collection of Objects in JDBC
It's not a secret anymore!