• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning a table into an array.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you have this code? What is it for?

if(rs.next()) {
System.out.println("ERROR_IN_DATABASE!"); }//
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you know the size of retrieve records from database then try this code



hope it helps.
 
Darren Jackson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have answered my own question here.

The answer is yes you can.

Here is how I did it:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic