Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:25:34+00:00 2026-06-05T15:25:34+00:00

I am using a Java app to read thru text files and output them

  • 0

I am using a Java app to read thru text files and output them to a SQL script and eventually a Mysql database. One of the columns is made-up of several lines of unformated text, including symbols like commas and apostrophes. To make matters more difficult I have purposely add new lines in order to keep the text readable. Is there a way to have MySQL ignore character combinations that will cause problems (mainly apostrophes, but I have know way of knowing what these blocks of text will contain) without breaking the newlines?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:25:34+00:00Added an answer on June 5, 2026 at 3:25 pm

    Read through this: http://dev.mysql.com/doc/refman/5.1/en/string-literals.html

    In a nutshell, these are the characters to replace

    \0  An ASCII NUL (0x00) character.
    \'  A single quote (“'”) character.
    \"  A double quote (“"”) character.
    \b  A backspace character.
    \n  A newline (linefeed) character.
    \r  A carriage return character.
    \t  A tab character.
    \Z  ASCII 26 (Control+Z). See note following the table.
    \\  A backslash (“\”) character.
    \%  A “%” character. See note following the table.
    \_  A “_” character. See note following the table.
    

    Here is what I have been using for a while in some Java programs where I can’t/don’t use Parameter Binding.

    public static String addSlashesSearchMode(String s) {
        return addSlashes(s, true);
    }
    
    public static String addSlashes(String s) {
        return addSlashes(s, false);
    }
    
    private static String addSlashes(String s, boolean search) {
        if (s == null) {
            return s;
        }
        String[][] chars;
        if(!search) {
            chars = new String[][ ]{
                    {"\\",  "\\\\"},
                    {"\0", "\\0"},
                    {"'", "\\'"}, 
                    {"\"",  "\\\""},
                    {"\b",  "\\b"},
                    {"\n",  "\\n"},
                    {"\r",  "\\r"},
                    {"\t",  "\\t"},
                    {"\\Z", "\\\\Z"}, // not sure about this one
                    {"%", "\\%"},     // used in searching
                    {"_", "\\_"}
            };
        } else {
            chars = new String[][ ]{
                    {"\\",  "\\\\"},
                    {"\0", "\\0"},
                    {"'", "\\'"}, 
                    {"\"",  "\\\""},
                    {"\b",  "\\b"},
                    {"\n",  "\\n"},
                    {"\r",  "\\r"},
                    {"\t",  "\\t"},
                    {"\\Z", "\\\\Z"}, // not sure about this one
            };
        }
        for (String[] c : chars) {
            s = s.replace(c[0], c[1]);
        }
        return s;
    }
    

    Usage: StringUtils.addSlashes("ke\rn\tny's\"\nnew li\\ne%"))

    output: ke\rn\tny\'s\"\nnew li\\ne\%

    output viewed in phpmyadmin:

    ke
    n    ny's"
    new li\ne\%
    

    Note: that I’m not really sure about the \Z implementation, perhaps someone can elaborate more on that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java app using a MySQL database through hibernate. The database is
I'm working on a GAE/Java app, and today I read about using app.yaml, queue.yaml,
I need to read a remote file using a java app, but the file
Good times! My Android app is trying to read simple text file, using usual
I am using mysql in my java base web app. I want to lock
I'm currently porting some work from MySQL to Google App Engine/Java. I'm using JDO,
I'm planning to implement a process using a java app. The process has various
I am using Facebook-java-api to integrate the Facebook app in java web app(Struts), i
I had a Spring based java app that connected to PayPal using their Soap
I'm developing both a Java app and J2EE webapp using Eclipse Europa on Mac

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.