Results 1 to 13 of 13
  1. #1
    cplmckenzie is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Cool Using a variable in a SELECT FROM WHERE query

    Hi,
    I have a java web app that accesses a MySQL database.

    It works well except that I am unable to to retrieve a rowset when I use

    the WHERE clause with a variable... as in

    SELECT * FROM tablename WHERE tablename.fieldname = variablename

    Any help is appreciated

    regards,

  2. #2
    fred is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default

    did you get some exception? or any error?

  3. #3
    cplmckenzie is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default Using a variable in a SELECT FROM WHERE query

    No. The query executed but it said "No Items Found" as though the variable was null.

    I was attempting to change the query string in a SessionBean setcommand.Value

    regards,

  4. #4
    leonard is offline Member
    Join Date
    Jul 2007
    Posts
    43
    Rep Power
    0

    Default

    please paste the code, probably the problem is in it

  5. #5
    daheri is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by cplmckenzie View Post
    No. The query executed but it said "No Items Found" as though the variable was null.
    Hey man,

    "No Items Found" means exactly what it says. Your query has returned 0 rows.
    In other words, either the table is empty, or your where clause is eliminating all results. Try running your query directly against the db to check.

  6. #6
    anki1234 is offline Member
    Join Date
    Dec 2007
    Posts
    28
    Rep Power
    0

    Default

    paste the code . May be u are not able to provide proper parameter in query corresponding to which u r making search

  7. #7
    rico16135 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    Use PreparedStatement to create your query. leave a '?' in place of your variable to insert. then use the method set() in the PreparedStatement class to insert your variable. There are setInt, setString, etc. for whatever type you are using. You should read up on PreparedStatement as well as all the Statement classes to see how they are used, as well as to determine which one to use.

  8. #8
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    Have you constructed the query string correctly?
    Java Code:
    "SELECT * FROM " + tablename + " WHERE " + tablename.fieldname + " = " + variablename;
    Last edited by DonCash; 04-22-2008 at 11:21 AM.
    Did this post help you? Please me! :cool:

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by DonCash View Post
    Java Code:
    "SELECT * FROM " + tablename + " WHERE " + tablename.fieldname + " = " + variablename;
    tablename.fieldname

    Here need only the field/column name right? No need to use the table name here. :)

  10. #10
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Hei Eranga, You have pointed out the error.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  12. #12
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    I thought he had wrote a Java application to connect to a SQL database and that was his string query to the db being sent within the code. Nice one Eranga.
    Did this post help you? Please me! :cool:

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Select Count
    By Apple2 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-29-2008, 09:02 AM
  2. JDBC select problem
    By abhiN in forum JDBC
    Replies: 0
    Last Post: 03-12-2008, 08:09 AM
  3. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 07:47 AM
  4. Internationalization: Select language from combo box
    By alejandrgarcia in forum AWT / Swing
    Replies: 0
    Last Post: 08-11-2007, 10:26 AM
  5. How to select/highlight an entire row in JTextArea
    By Valeriano in forum AWT / Swing
    Replies: 2
    Last Post: 05-28-2007, 11:20 AM

Posting Permissions

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