Escape character problems in sqlConnectionString

Here's the code I'm having problems with:

sqlQueryString += " WHERE " + filterField + " = '" + filter + "'";

//later I use it like this:

            dbStatement = dbConn.createStatement();
            dbResultSet = dbStatement.executeQuery(sqlQueryString);
            return true;

the problem is that java inserts escape characters (\) before the ' on the "filter" variable.

so sqlQueryString ends up looking like this:
        "select UserName from Users WHERE UserID = \'billybob\'"
and of course this throws an exception when it get to the database (Oracle). What I want the sqlQueryString to look like is:
        "select UserName from Users WHERE UserID = 'billybob'"

Seems to me I must be missing something cuz this is a pretty common operation I'm trying to do here. Help!

Thank you,

Jon
LVL 1
jonleehackerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
If you use a prepared statement then all escaping required is handled for you by the driver.
http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html
Mick BarryJava DeveloperCommented:
PreparedStatement ps = connection.prepareStatement("select UserName from Users WHERE UserID = ?");
ps.setString(1, id);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OviCommented:
set the connection url to use a specific encoding (usually charSet=utf-8):
jdbc:postgresql://localhost:5432/A_DATABASE?charSet=utf-8
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

jonleehackerAuthor Commented:
OOOOOOps,

While trying out your suggestions I found the problem (it was unrelated to my question).

Thanks for your responses, since I'm a novice they were both useful to me.

If I can figure out how I will split the points between the two.

Jon
jonleehackerAuthor Commented:
Sorry Ovi,

It made me choose only one as the official 'accepted answer' to I choose object since he was first.

Thanks again,

Jon
OviCommented:
No prob., thank you too.
Mick BarryJava DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.