• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Problem with quotes (escape characters)

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
String sql="insert into tableA values('"+insertthis+"')";
 
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One word: PreparedStatement
 
archana vishwanath
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(')

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have u tried implementing \' instead of directly applying those single quotes
 
Bear Bibeault
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, repeating myself, have you tried using a PreparedStatement where you don't need to worry about it in the first place?
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
We noticed he had no friends. So we gave him this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic