Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Mysql syntax error

  1. #1
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Mysql syntax error

    I'm having an issue with my javafx app when I try to retrieve data from my database. Below is the query string:
    Java Code:
    String sql = "SELECT SUM(PressCounter) AS TotalMolded, SUM(Rejects) AS TotalRejects, MONTH(ProductionDate) AS dMonth, ProductID"
    				+ "FROM productionlogs "
    				+ "WHERE  ProductID = '" + productID+ "' AND YEAR(ProductionDate) = '"+ year +"' "
    				+ "GROUP BY MONTH(ProductionDate), ProductID";
    Javafx is throwing a syntax error but when I run the same string in SQL Manager for Mysql the query works fine. I can't see what I'm doing wrong, here is the error I am getting.
    Java Code:
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'WHERE  ProductID = '10601' AND YEAR(ProductionDate) = '2016' GROUP BY MONTH(Produ' at line 1
    Does anyone have an idea what could be causing the error?

    Thanks in advance.
    -Fred

  2. #2
    caraldur is offline Member
    Join Date
    Feb 2015
    Posts
    27
    Rep Power
    0

    Default Re: Mysql syntax error

    Found my error, forgot to put a space between ProductID and From.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,954
    Rep Power
    7

    Default Re: Mysql syntax error

    Use this moment to learn about PreparedStatements. This error happen for the rest of your life if you keep using sql statements like that. And I am not even mentioning the SQL injection attacks you'll be suffering.
    jepoys1 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    jepoys1 is offline Member
    Join Date
    Apr 2017
    Posts
    5
    Rep Power
    0

    Default Re: Mysql syntax error

    Quote Originally Posted by SurfMan View Post
    Use this moment to learn about PreparedStatements. This error happen for the rest of your life if you keep using sql statements like that. And I am not even mentioning the SQL injection attacks you'll be suffering.
    Can you give me some examples on how to code good sql statements?

Similar Threads

  1. Replies: 12
    Last Post: 04-22-2014, 07:47 PM
  2. No more syntax error
    By ideyatech in forum Java Software
    Replies: 2
    Last Post: 04-22-2010, 04:20 AM
  3. Replies: 2
    Last Post: 08-31-2009, 02:50 PM
  4. Java JDBC/MySQL appropriate Syntax
    By thelinuxguy in forum Advanced Java
    Replies: 7
    Last Post: 02-10-2009, 07:57 PM
  5. syntax error
    By gabriel in forum New To Java
    Replies: 3
    Last Post: 08-03-2007, 03:26 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
  •