File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Syntax error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Syntax error" Watch "Syntax error" New topic

Syntax error

Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
HI i am trying to write a sql query and storing it in a empty string so that i can execute it later

So it should run like this in the application >> Select * from users where name=? and pass=?;

i have two variable that i have to check from the code with databse.

a string loginname and string password

SO here is what i am doing

But when i run application i am getting this error =(

R 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 'shen AND pass= abcd1234' at line 1
Matthew Brown

Joined: Apr 06, 2010
Posts: 4544

The syntax error is because you haven't got quotes round the strings in your SQL statement.

However, don't do it that way! Use parameterized queries. It solves this problem, and it prevents any nasty security problems (search for "Injection Attack" to see what your current version might be opening itself to).
Amritpal Singh Moomie
Ranch Hand

Joined: Sep 24, 2010
Posts: 32
thanks very much
i appreciate

i have one more question

How can i check whether the resultset has any row or not?

like is there any record for such query?
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

That has been answered in your other post:

Good luck.

OCUP UML fundamental and ITIL foundation
I agree. Here's the link:
subject: Syntax error
It's not a secret anymore!