Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: Query database

  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Query database

    Hi,

    I have a form with a text field that I write a ID number.
    But however it only returns the first row i my table.

    This is my code:
    Java Code:
    int id = Integer.parseInt(textID.getText());
            
            String query = "SELECT * FROM PersonTable WHERE ID = id";
    This code works when i query directly in the server.
    What could be wrong in my code?

  2. #2
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Query database

    Quote Originally Posted by asai View Post
    Hi,

    I have a form with a text field that I write a ID number.
    But however it only returns the first row i my table.

    This is my code:
    Java Code:
    int id = Integer.parseInt(textID.getText());
            
            String query = "SELECT * FROM PersonTable WHERE ID = id";
    This code works when i query directly in the server.
    What could be wrong in my code?
    "query" uses a single string? or an array of strings?

    regards
    dhilip

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Query database

    Quote Originally Posted by asai View Post
    Hi,

    But however it only returns the first row i my table.
    What could be wrong in my code?
    it's a single string, so it returns only one string

    regards
    dhilip

  4. #4
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Query database

    Yes, it should only receive the one row with id = the input from the text field...
    But it receive only the first row no matter what i write in the textfield.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default Re: Query database

    Quote Originally Posted by asai View Post
    Hi,

    I have a form with a text field that I write a ID number.
    But however it only returns the first row i my table.

    This is my code:
    Java Code:
    int id = Integer.parseInt(textID.getText());
            
            String query = "SELECT * FROM PersonTable WHERE ID = id";
    This code works when i query directly in the server.
    What could be wrong in my code?
    The text "id" in your query string has nothing to do with a variable with the same name; change that to:

    Java Code:
            String query = "SELECT * FROM PersonTable WHERE ID = "+id;
    or even better: use a PreparedStatement for that purpose. It has nothing to do with arrays of Strings or whatever; that is hogwash.

    kind regards,

    Jos
    noobplus likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Query database

    Thanks Jos, it worked like a charm!
    Java Code:
    String query = "SELECT * FROM PersonTable WHERE ID = "+id;

  7. #7
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Query database

    Quote Originally Posted by asai View Post
    Thanks Jos, it worked like a charm!
    Java Code:
    String query = "SELECT * FROM PersonTable WHERE ID = "+id;
    Java Code:
    String query = "SELECT * FROM PersonTable WHERE ID = "+id;
    can it accept more than one entity?

    thx
    dhilip
    Last edited by noobplus; 03-17-2012 at 04:12 PM.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default Re: Query database

    Quote Originally Posted by noobplus View Post
    Java Code:
    String query = "SELECT * FROM PersonTable WHERE ID = id";
    can it accept more than one entity?
    A Statement can return more than one enitity; why else would the return value be a ResultSet?

    kind regards,

    Jos
    noobplus likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Query database

    Quote Originally Posted by JosAH View Post
    A Statement can return more than one enitity; why else would the return value be a ResultSet?

    kind regards,

    Jos
    i mean a single string "query" can have many entities?
    ETA:got it, never-mind
    Last edited by noobplus; 03-17-2012 at 04:18 PM.

Similar Threads

  1. Database Query using Hibernate
    By javababy in forum Web Frameworks
    Replies: 0
    Last Post: 08-02-2011, 11:54 AM
  2. Replies: 5
    Last Post: 04-22-2010, 09:05 AM
  3. how to read a url parameter to query a database
    By kwesiaryee in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-01-2008, 02:38 PM
  4. Query in database
    By anki1234 in forum JDBC
    Replies: 3
    Last Post: 08-20-2008, 05:45 PM
  5. How to query data from database using SSL
    By mano in forum New To Java
    Replies: 0
    Last Post: 08-02-2007, 05:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •