aspose file tools*
The moose likes JDBC and the fly likes Returning a table into an array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Returning a table into an array." Watch "Returning a table into an array." New topic
Author

Returning a table into an array.

Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
I have been trying all night to do this.

I have a table on a server and I can get my code to either return one name anywhere in my array, or get one name to fill the whole array.

What I want is to get each name from the database is a new array field.

Here is my method I am using for this:



Currently I get the whole array filled with the name Mintaka.

If I change the SQL to be SELECT Name FROM Objects then I get a ERROR_IN_DATABASE! error.

Can anyone tell me why I cannot get this to do what I want it too?

Is it even possible?

Ultimatly I am trying to get a whole table returned into a 2D array.

But Im not going to run before I can walk...

Cheers

Darren
[ January 19, 2007: Message edited by: Darren Jackson ]
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Why do you have this code? What is it for?

if(rs.next()) {
System.out.println("ERROR_IN_DATABASE!"); }//
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by Darren Jackson:



rs.next() mean cursor move to the next record into your ResultSet Object or in if conditions its run only one time. its mean if you retrieve more than one records in your rs object then its not move to the next record and in your for loop you're filling the String Array with same cursor value.
[ January 19, 2007: Message edited by: Saif uddin ]

Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by Darren Jackson:


If I change the SQL to be SELECT Name FROM Objects then I get a ERROR_IN_DATABASE! error.


Its mean that record is still exist in your rs Object.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

if you know the size of retrieve records from database then try this code



hope it helps.
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
Thanks alot for your help guys.

It took me a while but its working now :-).

The problem I am having now is that I need some way of finding out how many rows my SQL table has.

Ive looked all over the Java API and cannot find anything.

Is there an SQL statement that will do this for me?

Or does anyone know any other method that I can use?

In the code above I know for a fact that the table I am using is 10 rows long, but I will not always know the size.

I did find the getMaxRows(); method in the Javax.sql API, does anyone know if this will do the job?

Cheers

Darren
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Darren,

What is your design? Limit to a specific number? Or your system is dynamic and can handle any number of data?
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
Originally posted by Jesus Angeles:
Darren,

What is your design? Limit to a specific number? Or your system is dynamic and can handle any number of data?


The database ultimatly has no limit.

The application is going to have an option for the user to add as my field into the database as possible.

Does this help?
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Correct me if I am wrong, but, your system is not just display-only, but is also an update system.

Why not use something like this:


Since you dont know how much many rows there are, try use an object which you dont need to specify immediately the size of the array - like the Vector. I also didnt see any API method that returns the number of rows in the ResultSet.
[ January 20, 2007: Message edited by: Jesus Angeles ]
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
Yes it is possible to update the database too.

I can sucessfully store the data in a 2D array now as I wanted to.

But ideally I need to get the number of rows in the table before I do that.

I have found the command "SELECT COUNT (*) FROM Objects".

However I cannot get it to implement into any JDBC code.

What I am after is a way of counting the number of rows in a table.
Darren Jackson
Ranch Hand

Joined: Jan 18, 2007
Posts: 42
I have answered my own question here.

The answer is yes you can.

Here is how I did it:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning a table into an array.