aspose file tools*
The moose likes JDBC and the fly likes Java JDBC jtable only displaying one record Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Java JDBC jtable only displaying one record" Watch "Java JDBC jtable only displaying one record" New topic
Author

Java JDBC jtable only displaying one record

Dene Lamprecht
Greenhorn

Joined: Jun 23, 2009
Posts: 14
Hallo

I am trying to display data from my ms SQL 2000 database in a jtable, the problem is that it only displays one record, though I know there should be 21 records. I think the problem has to do with the while (rs.next())??? but I just cant seem to fix it. Please help me out



thank you in advance
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

with the rows[i] loop inside the while(rs.next()) loop, you are overwriting the rows array each time ( i is reset to zero each time through the while loop) and MOVIEID.length is always 1 so each time through, you are re-creating a rows array with one element.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

As Tim pointed out the for and while loop seem redundant, generally you just do "while (rs.next()) {}" in JDBC. Also, note that the number of records in your ResultSet is unavailable in most DBMS, so you tend to need a dynamic array such as an ArrayList over a typed array of Object[][] since you may not know the size of the results. For example, if your result was corrected it would throw an index exception if there were more than 10 records.

Lastly, is it so hard to create a Movie class? I don't think I've seen a 3-dimensional array of Object[][] in a very long time. Better structure would be List<Movie> initialized using the ArrayList<Movie>() constructor.


My Blog: Down Home Country Coding with Scott Selikoff
Dene Lamprecht
Greenhorn

Joined: Jun 23, 2009
Posts: 14
Thank you guys for replying. I am very new to Java but I think I might have an idea of what you are saying. As far as
Scott Selikoff wrote:create a Movie class
is this what you meant:
?

is it possible that anyone can give me a link to a tutorial regarding the ArrayList, because all I'm able to find is tutorials that has to do with user input into ArrayLists. I am struggeling to understand what I must do to load the ArrayList into my table.
PS: I am not allowed to use vectors for this.

Thank you
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Vectors and ArrayLists are nearly identical, there's a million examples comparing them on the web.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java JDBC jtable only displaying one record