File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes select from DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "select from DB" Watch "select from DB" New topic

select from DB

alona ariel
Ranch Hand

Joined: Sep 09, 2008
Posts: 36
I have a DB of passenger and I want to create a new DB of the passenger that travel to london
I want to ask if my Algorithm is good only

Darryl Burke

Joined: May 03, 2008
Posts: 5115

  • Does it compile without error?
  • Does it run without error?
  • Does it give the desired results?
  • When making your post, did you see a button labeled 'Code'?

  • luck, db
    There are no new questions, but there may be new answers.
    Paul Clapham

    Joined: Oct 14, 2005
    Posts: 19973

  • You should use a PreparedStatement with parameters, rather than using string concatenation to build a query.
  • You should close your ResultSet and PreparedStatement after using them. A finally-block works well for that.
  • It's kind of weird to write a while-loop where you unconditionally return in the first time through the loop. Use an if-statement for this.
  • In a catch-statement it's better to print the whole stack trace, so you can see where the error occurred.
  • alona ariel
    Ranch Hand

    Joined: Sep 09, 2008
    Posts: 36
    I have another option to get the passenger that travel to london
    I ask if it is a good way

    Jan Cumps

    Joined: Dec 20, 2006
    Posts: 2565

    The SQL statement in your first attempt was better. Anf the while loop in your new attempt is better.
    Your new SQL query is invalid. You did not give a table name. (you need to select <from table>). And if you write a where clause in your query (justl like you did in your first example), you only get records where the city is london, and you don't have to perform the extra check in the while loop.

    And follow Paul's advise: close your resources after use, and use a PreparedStatement.

    OCUP UML fundamental and ITIL foundation
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 46397
    Why have you got a class called London? Why are you using the == operator to compare Strings?
    Jeanne Boyarsky
    author & internet detective

    Joined: May 26, 2003
    Posts: 33130

    Note the [code] button when editing your post that you can use to make the code display nicer.

    [OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
    I agree. Here's the link:
    subject: select from DB
    It's not a secret anymore!