wood burning stoves 2.0*
The moose likes JDBC and the fly likes Get data from my DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Get data from my DB" Watch "Get data from my DB" New topic
Author

Get data from my DB

Lenny Peter
Ranch Hand

Joined: Apr 22, 2008
Posts: 52

How can I get list of all cars in my DB? ;-)

I get only 1 car from the DB (Volvo).



This is my list:
Volvo
Citroen
Audi
BMW



Thanks
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Your code is doing exactly what it should, your while loop will only execute once...move the return statement outside the while loop
Lenny Peter
Ranch Hand

Joined: Apr 22, 2008
Posts: 52
I cannot.....


q cannot be resovled-------------------> return q;
Samuel March
Ranch Hand

Joined: Oct 28, 2009
Posts: 39
Move statement.close(); that out of the loop too, close it when your finnished with it.

...did you have the fish?!............................ No.
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Q cannot be resolved because it is not 'visible' from where you are returning it...declare it outside the loop and you will be good.

Also, Your code as it exists will not work as your expect...

try either change the method signature to return an String[] or use a StringBuffer to add all the results in the loop and at the end return the toString()

And also move the statement.close() too
Lenny Peter
Ranch Hand

Joined: Apr 22, 2008
Posts: 52
I have tried a different things, but it still not works..
don't know how to change the method signature to return an String[]...
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30382
    
150

Lenny Lee Petersen wrote:
How can I get list of all cars in my DB? ;-)

I get only 1 car from the DB (Volvo).



This is my list:
Volvo
Citroen
Audi
BMW



Thanks

I recommend using an ArrayList rather than an array so you don't have to keep track of size.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30382
    
150

Lenny Lee Petersen wrote:I have tried a different things, but it still not works..
don't know how to change the method signature to return an String[]...


or if you go with ArrayList
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

It would be too easy for us to actually give you the right code in the first place, but thats not the approach of this forum. We try to point you to the right direction so that you can figure it out yourself...more so when you are a beginner in Java.

I would suggest you to experiment with the tips given here...

ArrayList or List is the right approach here as Jeanne Boyarsky suggested in the earlier post.

Now that you know how to change the method signature...figure out what changes you need to make to return that type...either a java.util.List of the String[]...


Lenny Peter
Ranch Hand

Joined: Apr 22, 2008
Posts: 52
Thanks, it works now...

But I have one more question

This is my question:
In a real application, above prices would be retrieved
from a database, of course. How can I do this?
private final float CAR1_PRICE = 1.45F;
private final float CAR2_PRICE = 1.75F;





My codes:


I can get only price for last or first car in my DB.
Qt is OK.
Total cost is OK.





Thanks
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

First of all use JSP's instead of out.println.

Try taking the db query logic part out of the main while loop
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Get data from my DB