Solved

Escape character problems in sqlConnectionString

Posted on 2003-11-05
7
443 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:jonleehacker
  • 3
  • 2
  • 2
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9692327
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
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 9692332
PreparedStatement ps = connection.prepareStatement("select UserName from Users WHERE UserID = ?");
ps.setString(1, id);

0
 
LVL 9

Assisted Solution

by:Ovi
Ovi earned 50 total points
ID: 9693752
set the connection url to use a specific encoding (usually charSet=utf-8):
jdbc:postgresql://localhost:5432/A_DATABASE?charSet=utf-8
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:jonleehacker
ID: 9694731
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
0
 
LVL 1

Author Comment

by:jonleehacker
ID: 9694758
Sorry Ovi,

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

Thanks again,

Jon
0
 
LVL 9

Expert Comment

by:Ovi
ID: 9695545
No prob., thank you too.
0
 
LVL 92

Expert Comment

by:objects
ID: 9697286
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now