• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retriving Data in a tabular form

 
Patrick Mugabe
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much, I will try it out tomorrow and let you know how it goes.
 
Patrick Mugabe
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic