Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Escape character problems in sqlConnectionString

Posted on 2003-11-05
7
Medium Priority
?
467 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 200 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

926 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