Solved

Escape character problems in sqlConnectionString

Posted on 2003-11-05
7
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

734 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