| Author |
Passing array of objects......ResultSet
|
Anchit Kalra
Greenhorn
Joined: Jan 25, 2009
Posts: 9
|
|
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
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Anchit Kalra
Greenhorn
Joined: Jan 25, 2009
Posts: 9
|
|
|
Let me rewrite the code and then post it again
|
 |
Anchit Kalra
Greenhorn
Joined: Jan 25, 2009
Posts: 9
|
|
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!!
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
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.
|
Steve
|
 |
Anchit Kalra
Greenhorn
Joined: Jan 25, 2009
Posts: 9
|
|
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...
|
 |
denis sorn
Ranch Hand
Joined: Apr 30, 2008
Posts: 33
|
|
|
You also have to close ResultSet, Statement and Connection in that order or you'll encounter problems sooner or later.
|
 |
 |
|
|
subject: Passing array of objects......ResultSet
|
|
|