• 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

Get data from my DB

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 128
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot.....


q cannot be resovled-------------------> return q;
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[]...
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic