• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection of Objects in JDBC

 
Abhin Balur
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



AccessTableDAO.java:-


import java.sql.*;
import java.io.*;
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


try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

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


stmt = con.createStatement();

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

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

coll.add(tab);


}




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



return coll;


}

}



Client.java (Servlet):-




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


public class Client extends HttpServlet

{

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


{
res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.print("<html><head>");
out.print("</head><body>");

out.print("<code><pre>");

// create the required DAO Factory
DAOFactory accessFactory =
DAOFactory.getDAOFactory(DAOFactory.ACCESS);

// 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)

{
out.println("Heloo");


}



Iterator it= tablelist.iterator();


//out.println("Heloo1");
while(it.hasNext())

{
out.println("Heloo1");
Table element=(Table)it.next();

out.print(element.srno);
out.print("\t");
out.print(element.name);
out.print("\t");
out.print(element.std);
out.print("\t");
out.print(element.roll);
out.print("\n");

}

out.print("</body></html>");
out.close();


}
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic