aspose file tools*
The moose likes JDBC and the fly likes select from DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "select from DB" Watch "select from DB" New topic
Author

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
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

  • 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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18154
        
        8

  • 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
    Bartender

    Joined: Dec 20, 2006
    Posts: 2477
        
        7

    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
    youtube channel
    Campbell Ritchie
    Sheriff

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

    Joined: May 26, 2003
    Posts: 29287
        
    140

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


    [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
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: select from DB
     
    Similar Threads
    Prb in compiling the JSP
    Pizza and Servlets
    passing parameters using Prepared Statement
    JTable date edit problem
    ORA-01000: maximum open cursors exceeded