Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

select from DB

 
alona ariel
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • 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'?
  •  
    Paul Clapham
    Sheriff
    Pie
    Posts: 20750
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • 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
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have another option to get the passenger that travel to london
    I ask if it is a good way





     
    Jan Cumps
    Bartender
    Posts: 2584
    11
    C++ Linux Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48394
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why have you got a class called London? Why are you using the == operator to compare Strings?
     
    Jeanne Boyarsky
    author & internet detective
    Marshal
    Posts: 34084
    337
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Note the [code] button when editing your post that you can use to make the code display nicer.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic