I had recently faced this problem. Here is a method that I wrote to deal
with it.

/* This method checks passed String str for single quotes and escapes them
     returning a new SQL-worthy string.
 */
private String formatString(String str)
{
     java.lang.String newStr = "";

     // tokenize by "'"
     java.util.StringTokenizer tok = new
java.util.StringTokenizer(str,"'");
     while (tok.hasMoreTokens())
     {
          newStr = newStr.concat(tok.nextToken());
          if(tok.hasMoreTokens())
          {
               newStr = newStr.concat("''");
          }
     }

     return newStr;
}

Good luck!

Elena Tsifrina
SYSCOM, Inc.
www.syscom.com
410-539-3737 x1322




                    "Tiffany C."
                    <tiffanyc1012@YAH       To:     [EMAIL PROTECTED]
                    OO.COM>                 cc:
                    Sent by: A              Subject:     search and replace for single 
quotes
                    mailing list
                    about Java Server
                    Pages
                    specification and
                    reference
                    <JSP-INTEREST@JAV
                    A.SUN.COM>


                    12/17/01 04:10 PM
                    Please respond to
                    A mailing list
                    about Java Server
                    Pages
                    specification and
                    reference






Hello,

I have a jsp file with a html form in it.  Once the
form is completed and submitted: the file is sent back
to the same jsp file, a sql statement is made from the
information entered in the form, and finally the sql
statement is sent to the MySQL database.

If someone enters a single quote or ' in the form the
sql statement breaks.  I've found that adding '' in
place of ' will solve the problem.  For example
O'Kelly will break the sql but O''Kelly will work.

How can I search for single quotes and replace them
with two single quotes?  Is there a Java class that is
applicable?

Your help is appreciated!!  Thank you.

Sincerely,
Tiffany

__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to