aspose file tools*
The moose likes JDBC and the fly likes Problem with quotes (escape characters) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with quotes (escape characters)" Watch "Problem with quotes (escape characters)" New topic
Author

Problem with quotes (escape characters)

archana vishwanath
Ranch Hand

Joined: Mar 09, 2004
Posts: 39
Hi friends!

This is my code::
String insertthis="This is a s'amp'le test";
String sql="insert into tableA values(insertthis)";
ResultSet rs=stmt.execSQL(sql);
The upperquotes in the text which im trying to insert is creating
problem.Can any one throw me some code to effectively use escape characters
i.e replacing the upperquotes with the the appropriate escape characters

Thanx in advance,
Gowrishankar Mudaliar
Ranch Hand

Joined: Oct 20, 2001
Posts: 39
Try
String sql="insert into tableA values('"+insertthis+"')";
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

One word: PreparedStatement


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
archana vishwanath
Ranch Hand

Joined: Mar 09, 2004
Posts: 39
Thanx for all your replies
You have wrongly interpreted my question
String insertthis="This is a s'amp'le test";
String sql="insert into tableA values('"+insertthis+"')";
The query is fine.The actual problem is like this,
Since there theres some singlequotes(') in the text which im trying to insert
im getting sql exception.Now I want any one of you guys to tell me,how to escape the singlequotes(')

ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
have u tried implementing \' instead of directly applying those single quotes
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Or, repeating myself, have you tried using a PreparedStatement where you don't need to worry about it in the first place?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Listen to the Bear. People often think that the only use for a PreparedStatement is when you're using the same query many times. However this is a good example of another case when you should consider using a PreparedStatement.
If you move between databases, you'll quickly find that some use single quotes for delimiters, some use double quotes, and other support either. Handling nested single and/or double quotes is tricky.
The other "hard" data type is the date or timestamp. Every database has a different format for how you specify the value.
If you use a PreparedStatement, you don't have to worry about what delimiters are used with character data, what escape character should be used with quotes [or other special characters], or what format your date or time needs to be in. The drivers handle that auto-magically. And your code becomes much more portable between different databases.
archana vishwanath
Ranch Hand

Joined: Mar 09, 2004
Posts: 39
Thanx for Bear ,William and every one,
This is the code I tried,
String psql="insert into labgeneral (labgeneral_labid,labgeneral_slogan,labgeneral_logo,labgeneral_image,labgeneral_imagetext,labgeneral_para1,labgeneral_para2)"+
"values(?,?,?,?,?,?,?)";
out.println(psql);
PreparedStatement pstmt=con.prepareStatement(psql);
int labidi=Integer.parseInt(labid);
pstmt.setInt(1,labidi);
pstmt.setString(2,T1);
pstmt.setString(3,logoname);
pstmt.setString(4,imagename);
pstmt.setString(5,T2);
pstmt.setString(6,S1);
pstmt.setString(7,S2);
pstmt.executeUpdate();
I am able to insert the string with quotes sucessfully,but while retriving im not getting that upper quotes,insted I get some symbol [] like this in the place of quotes.How to overcome this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with quotes (escape characters)