File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java SQL Like statement not working

 
mithesh raj
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a mini application and I have to retrieve and save USER_ID in a table.

It works like this; I input the email address of the user and then the statement has to retrieve USER_ID and save it into a table.

I am having problem with my SQL Like statement. Its just that I have to insert only the first part of the email address. For example, "thomas@yahoo.com". I insert only "thomas"; not remaining part.

Here are sample of the statement and resulting message I am getting in my Java console:

Code:

String ea = txt_email.getText();
String lid = "SELECT USER_ID FROM user WHERE email_address Like '"+ea+"%' ";
Error Message:

Error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression ''SELECT USER_ID FROM user WHERE email_address Like 'thomas%' ''.
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
Your question has nothing to do with Swing/GUI and has been moved to a more appropriate forum.
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The wildcard character in MS Access is '*'.
Replace '%' with '*' and try.
Thanks.
 
mithesh raj
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friend..it is not working even with wildcard *
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the error message?
 
mithesh raj
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Error: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.


this time I have put it like this::
String lid = "SELECT USER_ID FROM user WHERE email_address Like '*"+ea+"*' ";
 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think MS Access uses double quotes - not single quotes - as text delimiter. Could that be the problem?

If you use PreparedStatement, you don't have to ponder about these database-specific text, number and date conventions.
 
mithesh raj
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did try with PreparedStatement...but instead of saving the User ID in the table, it is saving something like "sundbc:...." inside column cells.
 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I don't understand what is happening.

What did you change in your program?
What table are you speaking about? MS Access DB table, or a JTable in your application?

Perhaps you should show us the relevant code. If you do, please post it using the code tags and read the SSCCE link first.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic