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.....
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.
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.