• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Passing array of objects......ResultSet

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

well I am writing a program in which I have Two Classes(atleast for this Q) The Main class has the Gui and DB_conn class has the database connectios and queries.

The problem ocours when i need to return the array of tpe Objects from DB_conn to Main. the function "tmodel.addRow(data1);" takes an array of objects But if I dirrcly pass this Only the last element is shown........... here is the code.....


Please help
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your loop over "i" retrieves many different arrays and assigns them to the variable "data1", but it isn't until after the loop over "i" runs that you call addRow(). That means you only call addRow() on the last value that the array takes during the loop over "i".

But I'm pretty confused, frankly, as to what the various variables here represent. You may need to make more changes.
 
Anchit Kalra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me rewrite the code and then post it again
 
Anchit Kalra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code shows only the last row so how do i pass all the rows tom my Main Class
p.s sorry for using so many variables!!
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You loop over all the rows, put keep overwriting the same positions inside the data array, so yeah, only the last row will be available. You really need to redesign. Here are some options:
1) Use a 2D Object array:

Then when iterating over the result:


2) Make the data array long enough to hold all the rows and columns, so instead of Object[] data = new Object[colcount] it would be Object[] data = new Object[rowCount * colCount]; Then you have to keep track of what your offsets are in terms of both row and column:


3) Make a DataTransferObject which has a meaningful representation of the Row and fill in the correct values from the result set. Then pass back an array of the data transfer object. This would make handling the data a lot more maintenance friendly and understandable. As an example, if your database row represents the information about a Client, and the columns where:
[First Name, Last Name, Company, Position]
then your DTO might look like:

And when you gather data from the database it could look like:

Of course it would be a lot easier to pass around a generic list of type Client, or a Client[] instead of Object[] at this point...

Of the three options, 2) is the worst, and 3) is the best, in my opinion.
 
Anchit Kalra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys!!!

well i figured it out but have used the second way which is kind of !@#$@!#$
I will try doing it with making the class and then put the input in the variables....tht looks intresting...
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also have to close ResultSet, Statement and Connection in that order or you'll encounter problems sooner or later.
 
reply
    Bookmark Topic Watch Topic
  • New Topic