File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Retriving Data in a tabular form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Retriving Data in a tabular form" Watch "Retriving Data in a tabular form" New topic
Author

Retriving Data in a tabular form

Patrick Mugabe
Ranch Hand

Joined: Jan 08, 2002
Posts: 132
Ranchers!
I have a very small database which I am using to learn more about JDBC.
I am able to retrieve data but I would want it to come out in a tabular form. How can I do this.
Please can someone assist, probably with the aid of a simple example.
Thanks
Nagendra Prasad
Ranch Hand

Joined: Jul 11, 2002
Posts: 219
By saying 'Tabular Form', i assume u mean display the result set u have obtained in a tabular form. For this u would need to use either servletsor JSP... and as such would be a question for those forums.
The result set u have obtained can be transformed (for the lack of a better word).. into a vector.
The servlet/JSP can iterate thru this vector and generate the relevant HTML to display like a table.


Best Regards,<br />Nagendra Prasad.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Is 100 lines a simple example? This class gives you the framework of a GUI application. It displays a table and fills it with the results of a query. All the query code is commented out in the interest of showing a working example first. You should be able to make the indicated changes and get it to work with your database.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Woah. You will probably want to move the declarations of Connection, Statement and ResultSet to before the try block and set them to null. That's what I get for writing commented-out code.
Patrick Mugabe
Ranch Hand

Joined: Jan 08, 2002
Posts: 132
Thanks so much, I will try it out tomorrow and let you know how it goes.
Patrick Mugabe
Ranch Hand

Joined: Jan 08, 2002
Posts: 132
Hi,
The solution seems to work but I am having a problem on displaying the actual data from the table I am referencing.
It's coming from this section of code:
// get the data from the result set
for (int i = 1; i <= 15; i++){
// replace for loop with while(rs.next())
Vector v = new Vector();
for (int j = 1; j <= colCount; j++){
//v.add(""+i+","+j); // replace with v.add(rs.getObject(i))
v.add(rs.getObject(i))
}
data.add(v);
}
If I replace the for with a while loop, what then comes as an argument in:
v.add(rs.getObject(i)).
I am convinced this will work, I am just missing one simple thing.
Please assist
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

The code to iterate through the result set should look like this (I had the loop variable "j" wrong, don't forget to either hardcode the colCount or get it out of the ResultSetMetaData):
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retriving Data in a tabular form