Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2006
    Posts
    41

    Unanswered: Help with search query

    Hi,

    I'm having trouble writing the java code for the following mySql query:

    SELECT student_id, first_name, last_name
    FROM student
    WHERE student_id = given student_id;

    I can do it for a set id but need it to work when the student_id is given as a method parameter.

    So far, I have:

    Code:
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT student_id, first_name, last_name " + "FROM student " + "WHERE student_id = " + "329012");

    but need something like:

    Code:
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT student_id, first_name, last_name " + "FROM student " + "WHERE student_id = " + '"student_id"');
    I'm having some problem with the quotation marks. I need to student_id to be input as a parameter but the whole query to be read as a String. I'm currently getting

    Unclosed character literal and Unclosed String literal error messages.

    Could someone please help.

  2. #2
    Join Date
    Jan 2005
    Posts
    362

    PreparedStatement

    Last edited by dimis2500; 07-10-06 at 07:30.

  3. #3
    Join Date
    Jul 2006
    Posts
    8
    Like dimis2500 said you should use Prepared Statements or better Callable Statements for Stored Procedures.

    But... some times is easy to write a query like yours for testing ...
    or when a query can be polymorphic, if I am allow to say so.

    Almost every dbms can use the ' like a ", you can use the syntax:
    ... + "WHERE student_id = '" + student_id + "'"
    but only when there is no other option
    Last edited by HackmanC; 07-11-06 at 19:15.

Posting Permissions

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