Hi all,
I have a string user_name and it is saved in table with single
qoutes, i mean if you view the table there are single qoutes on both
sides of user_name like 'amir', it is saved with single qoutes. The
problem is how can i write sql query in java with = comparison, i'm
using
select * from sasuser.Followup where user_name= ' 'amir ' ';.
I need to use single qoutes for the query, coz variable is string but
how can i use single qoutes which are with the name itself.
Thanx.
jcsnippets.atspace.com - 13 May 2006 16:36 GMT
> Hi all,
> I have a string user_name and it is saved in table with single
[quoted text clipped - 6 lines]
> I need to use single qoutes for the query, coz variable is string but
> how can i use single qoutes which are with the name itself.
Double those single quotes - then the database will treat them as a single
quote, and not as a start or end quote.
select * from table where name = ''amir''
should thus become
select * from table where name = '''amir'''
Best regards,
JayCee
--
http://jcsnippets.atspace.com/
a collection of source code, tips and tricks
Rhino - 13 May 2006 16:46 GMT
> Hi all,
> I have a string user_name and it is saved in table with single
[quoted text clipped - 6 lines]
> I need to use single qoutes for the query, coz variable is string but
> how can i use single qoutes which are with the name itself.
When you say "table", I assume you mean a database table, not a JTable.
I've never seen a professional application where single quotes occurred in a
name column except for names like O'Connell. In other words, a professional
database would store and display the names like this:
Name
-------
JONES
SMITH
O'CONNELL
AZIZ
_not_ like this:
Name
-------
'JONES'
'SMITH'
'O''CONNELL'
'AZIZ'
Why was the data saved with single quotes in the first place? The only good
reason to put single quotes in a database record that refers to a person is
if the name is actually spelling with a single quote in it, like O'Connell.
Otherwise, I can't see any reason to put single quotes in the database
record in the first place. You've just found the best reason NOT to use
single quotes in your data: it makes it harder to find it again with your
queries!
Can you update or reload the data so that it doesn't contain single quotes?
This should eliminate your problem for all but a handful of rows that have a
name like O'Connell. Names containing a single quote can be handled quite
easily as well. For example:
select * from Employee where lastname = 'O''CONNELL'
[That is a single quote, the letter O, two consecutive single quotes,
CONNELL, and another single quote.]
--
Rhino
Bjorn Abelli - 13 May 2006 17:35 GMT
"Khan" wrote...
> I have a string user_name and it is saved in table with single
> qoutes, i mean if you view the table there are single qoutes on both
[quoted text clipped - 5 lines]
> I need to use single qoutes for the query, coz variable is string but
> how can i use single qoutes which are with the name itself.
I think you know how to create a string which has single quotes as
characters included, but are using concatenation when creating the query
strings, right?
One simple solution to your problem, is to use parameterized statements
instead of concatenation. Something like this:
String userName = ... ;
PreparedStatement ps =
connection.prepareStatement(
"select * from sasuser.Followup where user_name = ? ");
ps.setString(1, userName );
ResultSet rs = ps.executeQuery();
/// Bjorn A