Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Escape character problems in sqlConnectionString

Posted on 2003-11-05
7
Medium Priority
?
461 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 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

722 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