It's not a secret anymore!*
The moose likes JDBC and the fly likes Java SQL Like statement not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Java SQL Like statement not working" Watch "Java SQL Like statement not working" New topic
Author

Java SQL Like statement not working

mithesh raj
Greenhorn

Joined: Nov 04, 2012
Posts: 10
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

Joined: Jul 26, 2007
Posts: 10170
    
    8

Welcome to the Ranch.
Your question has nothing to do with Swing/GUI and has been moved to a more appropriate forum.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

The wildcard character in MS Access is '*'.
Replace '%' with '*' and try.
Thanks.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
mithesh raj
Greenhorn

Joined: Nov 04, 2012
Posts: 10
Dear friend..it is not working even with wildcard *
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Whats the error message?
mithesh raj
Greenhorn

Joined: Nov 04, 2012
Posts: 10

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

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Nov 04, 2012
Posts: 10
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

Joined: Aug 22, 2010
Posts: 3606
    
  60

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.
 
Don't get me started about those stupid light bulbs.
 
subject: Java SQL Like statement not working
 
Similar Threads
ODBC-JDBC connection and Looping
ODBC - JDBC connection and Updating MY SQL
How to link JSP to database file (Access 2007)
java.sql.SQLException-Need help to resolve.
INSERT Issue