• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get data from my DB

 
Lenny Peter
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 128
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot.....


q cannot be resovled-------------------> return q;
 
Samuel March
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move statement.close(); that out of the loop too, close it when your finnished with it.
 
Raj Chila
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all use JSP's instead of out.println.

Try taking the db query logic part out of the main while loop
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic