Results 1 to 15 of 15

Thread: MySQL problem

  1. #1
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default MySQL problem [Solved]

    Hello, y'all.
    The question is - where is syntax mistake?
    Java Code:
    st = "select * from schedule left join students on schedule.Students_id = students.id HAVING schedule.courses_id=?";
    PreparedStatement s=dbConnection.prepareStatement(st);
    s.setInt(1, Integer.parseInt(paramArray.get(0).toString()));
    Last edited by indra-uolles; 08-07-2010 at 08:12 AM. Reason: Solved

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The exception tells you where. Maybe if you were to post that we could help you correct it.

  3. #3
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 2"

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Should that be a WHERE rather than a HAVING?

  5. #5
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    I tried WHERE already and had the same result.

  6. #6
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    when I query using MySQL Workbench, this works fine:
    select * from schedule left join students on schedule.Students_id = students.id HAVING schedule.courses_id=1

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

    Default

    Quote Originally Posted by indra-uolles View Post
    I tried WHERE already and had the same result.
    According to this page the syntax of your SQL query is indeed incorrect.

    kind regards,

    Jos

  8. #8
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by indra-uolles View Post
    when I query using MySQL Workbench, this works fine:
    select * from schedule left join students on schedule.Students_id = students.id HAVING schedule.courses_id=1

    yes, but HAVING is usually used with GROUP-BY-clause, so if you don't have any grouping function in the SELECT-clause like (count, min, max) use WHERE.

  9. #9
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    When debugger reaches the line
    Java Code:
    s.setInt(1, Integer.parseInt(paramArray.get(0).toString()));
    I evaluate the expression Integer.parseInt(paramArray.get(0).toString()) and I see that it's "(int) 1".
    So it seems to be ok here too...

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Just to double check, is courses_id an inte and not a varchar (for example)?

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

    Default

    Quote Originally Posted by Tolls View Post
    Just to double check, is courses_id an inte and not a varchar (for example)?
    Type clashes should never cause syntax errors because the type system hits in after the syntax checks (parsing) has been done.

    kind regards,

    Jos

  12. #12
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    yes, it's int.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by indra-uolles View Post
    "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 2"
    The other indications given above are also correct, but from this error, let me guess, you are doing
    Java Code:
    s.execute(st);
    or
    Java Code:
    s.executeQuery(st);
    Well, don't include the argument/parameter in that call.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Type clashes should never cause syntax errors because the type system hits in after the syntax checks (parsing) has been done.

    kind regards,

    Jos
    Ah yes, good point.
    I was flailing a bit, it must be said.

    Quote Originally Posted by masijade View Post
    The other indications given above are also correct, but from this error, let me guess, you are doing
    Java Code:
    s.execute(st);
    or
    Java Code:
    s.executeQuery(st);
    Well, don't include the argument/parameter in that call.
    Ah.
    I wonder if we have a winner here?

  15. #15
    indra-uolles is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Problem connecting to mysql database
    By Kelly in forum JDBC
    Replies: 18
    Last Post: 07-22-2010, 06:40 AM
  2. Mysql LIKE clause problem
    By stalkerism in forum JDBC
    Replies: 1
    Last Post: 07-31-2009, 10:01 AM
  3. Eclipse / Mysql problem
    By jdevm in forum Eclipse
    Replies: 0
    Last Post: 10-09-2008, 01:16 AM
  4. mysql driver problem
    By mokingsu in forum JDBC
    Replies: 4
    Last Post: 01-17-2008, 05:27 PM
  5. Mysql problem
    By Nick15 in forum JDBC
    Replies: 2
    Last Post: 05-15-2007, 05:07 PM

Tags for this Thread

Posting Permissions

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