| 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: 8436
|
|
Welcome to the Ranch.
Your question has nothing to do with Swing/GUI and has been moved to a more appropriate forum.
|
[Donate a pint, save a life!] [How to ask questions] [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
Bartender
Joined: Aug 22, 2010
Posts: 2330
|
|
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 "sun dbc:...." inside column cells.
|
 |
Martin Vajsar
Bartender
Joined: Aug 22, 2010
Posts: 2330
|
|
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.
|
 |
 |
|
|
subject: Java SQL Like statement not working
|
|
|