Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Adding slash "\" character before quote "'" in a query

Adding slash "\" character before quote "'" in a query Adding slash " \ " character before quote " ' " in a query During the inserting the records in the database if user enters the phrases like "What ' s your name?", database gives the error due

Tutorial Details:

to the presence of the " ' " quote. So it is necessary to add slashes before the quote. For example if the user enters "What's your name?" then we will have to change it to "What \' s your name?" by placing the " \ " character before the quote " ' ". Following program do this:
import java.util.*;
public class quote{
public String addSlashes(String str){
if(str==null) return "";
StringBuffer s = new StringBuffer ((String) str);
for (int i = 0; i < s.length(); i++)
if (s.charAt (i) == '\'')
s.insert (i++, '\\');
return s.toString();
}
public static void main(String args[]) {
quote qt=new quote();
System.out.println(qt.addSlashes("What's your name?"));
}
}
If you run the above code addSlashes function will add slashe (" \ ") before the quote " ' " and the out put will be shown on the console.
You can use the following function in your java application to add the slashes.
public String addSlashes(String str){
if(str==null) return "";
StringBuffer s = new StringBuffer ((String) str);
for (int i = 0; i < s.length(); i++)
if (s.charAt (i) == '\'')
s.insert (i++, '\\');
return s.toString();
}
Next time I will show you how to use this in java applications.


 

Rate Tutorial:
http://www.roseindia.net/newsletter/addslashbeforquote.shtml

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Adding slash "\" character before quote "'" in a query

View Tutorial:
Adding slash "\" character before quote "'" in a query

Related Tutorials:

Reading textual data: Fun with streams - JavaWorld - April 1999
Reading textual data: Fun with streams - JavaWorld - April 1999
 
Jini: New technology for a networked world - JavaWorld June 1999
Jini: New technology for a networked world - JavaWorld June 1999
 
Optimize a query on a Map - JavaWorld November 2000
Optimize a query on a Map - JavaWorld November 2000
 
Take control of the servlet environment, Part 3 - JavaWorld January 2001
Take control of the servlet environment, Part 3 - JavaWorld January 2001
 
Plant your data in a ternary search tree - JavaWorld February 2001
Plant your data in a ternary search tree - JavaWorld February 2001
 
Design for performance, Part 2: Reduce object creation - JavaWorld February 2001
Design for performance, Part 2: Reduce object creation - JavaWorld February 2001
 
Survival of the fittest Jini services, Part 1 - JavaWorld April 2001
Survival of the fittest Jini services, Part 1 - JavaWorld April 2001
 
Matchmaking with regular expressions - JavaWorld July 2001
Matchmaking with regular expressions - JavaWorld July 2001
 
Mix protocols transparently in Struts
Mix protocols transparently in Struts
 
Create your own type 3 JDBC driver, Part 3
Create your own type 3 JDBC driver, Part 3
 
Java's character and assorted string classes support text-processing
Java's character and assorted string classes support text-processing
 
Servlet 2.4: What's in store
Servlet 2.4: What's in store
 
Simple classes for JDBC
Simple classes for JDBC
 
Simple Object Persistence with the db4o Object Database
Simple Object Persistence with the db4o Object Database. db4o has been chosen for applications in embedded systems in which zero administration, reliability, and low footprint are critical features. In Germany, BMW Car IT, for example, uses it in an embed
 
Lucene in Action
Lucene in Action Lucene is a gem in the open-source world--a highly scalable, fast search engine. It delivers performance and is disarmingly easy to use. Lucene in Action is the authoritative guide to Lucene. It describes how to index your data, includin
 
Request bean ver. 1.5
Java bean allows you to proceed GET/POST requests to the specified host (cgi-script/servlet). So you can for example obtain contents of some page and use extracted information in your own jsp page.
 
Adding search to your applications
The Lucene search engine is an open source, Jakarta project used to build and search indexes. Lucene can index any text-based information you like and then find it later based on various search criteria.
 
What is the correct form of a MIDlet suite version number?
One character can be the difference between a successful installation and a failed attempt. Check your knowledge of MIDlet versioning.
 
Adding slash "\" character before quote "'" in a query
Adding slash "\" character before quote "'" in a query Adding slash " \ " character before quote " ' " in a query During the inserting the records in the database if user enters the phrases like "What ' s your name?", database gives the error due
 
Welcome! to the News letter archive
Welcome! to the News letter archive News Letter Archive Date Subject Dec-29-2001 Adding slash " \ " character before quote " ' " in a query
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.