This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Escape characters for variables in sql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Escape characters for variables in sql" Watch "Escape characters for variables in sql" New topic
Author

Escape characters for variables in sql

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
HI All,

I am trying to execute a query on Oracle db. I am unable to substitute my variables in the sql statement. The sql is:

select prod_type_nm from prod_type_dim
where (prod_type_nm like ':INTNL_DEST%' OR prod_type_nm Like ':INTNL_DOMESTIC_DEST%' );

:INTNL_DEST and :INTNL_DOMESTIC_DEST are the variables which should be replaced. However this sql does not work, when I execute it using hardcoded values its fine.

My Dao extends NamedParameterJdbcDaoSupport of the Spring Framework.

Any help will be appreciated. I can provide more details if needed.

Many thanks!
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19747
    
  10

I wouldn't try to do that. I prefer to use features already built into Java when they exist, rather than features that don't. In this case the feature in question is a PreparedStatement. Define your query like this:and use the setString() method to set the parameters to something like "42%".
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Escape characters for variables in sql
 
It's not a secret anymore!