• Post Reply Bookmark Topic Watch Topic
  • New Topic

java query  RSS feed

 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guy,

StringBuffer query = new StringBuffer("SELECT SUM(AAAAA), SUM(DDDDDDD) ");
query.append(" FROM "+Constants.emp+" WHERE CAT
='").append(category).append("' AND YEAR(DATE)=").append(year);


query.append(" AND MONTH(DATE)IN").append("(").append(monthStr).append(")");// what is the wrong in the syntax for monthStr in(1,2,3)

The error I am getting is

java.sql.SQLException: Syntax error or access violation: 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 '1,2,3)' at line 1

can anyone help me please ?

thanks
 
Paul Clapham
Sheriff
Posts: 22843
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the error message, I would say there is something wrong with the syntax of the SQL statement which you are producing with that code. So the first step should be to look at it.

(Hint: System.out.println(query)...)

If you still can't see the problem, then post the query here. Not the code that generates it, the query itself.
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to print it on the console. But it is not going there, it is immediately firing the sql Exception check the right syntax for '1,2,3') at line 1 .
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: Instead of concatenating strings to form a query, it is much better to use a PreparedStatement. By using a PreparedStatement, you let the JDBC driver figure out exactly how to pass parameters to the SQL statement, and you are protected against SQL injection (a security attack method).

An example:
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by lakshmi manepally:
I have tried to print it on the console. But it is not going there, it is immediately firing the sql Exception check the right syntax for '1,2,3') at line 1 .


Do you print it before running the executeQuery method?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!