Can not store the correct XML Schema facet value in MySQL table

Hi experts

I have a problem with storing facet value in MySQL table. Here is the code:

public void insertFacet(int vid, int pathid, int pPathid, String fName, String fValue) throws SQLException {
        Connection con = startDBConnection(server_name,databse_name,username,password);
        try {           
            Statement statement = con.createStatement();
            String query = "INSERT INTO facet VALUES("
                    + vid + ", " + pathid + ", " + pPathid + ", '" 
                    + fName + "', '" + fValue + "') ";
        } catch (SQLException e) {

Open in new window

I use java language. When I parse XML Schema, it stores the value of the 'pattern' facet but ignore the backslash symbol \
For example the facet
<xs:pattern value="\d{1,2}"/>

Open in new window

is stored like this:

Open in new window

Any suggestion why this is happening and how to solve it?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use a PreparedStatement. It will handle escaping correctly for you
abaqasahAuthor Commented:
Could you explain more please.
Do you mean the replaceAll() function that is used for java string?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

abaqasahAuthor Commented:
Thanks CEHJ

I make a simple alteration and the problem is solved.

I add the replace function like this:
fValue = fValue.replace("\\", "\\\\");

Open in new window


You don't need to escape it yourself - the PreparedStatement will do it for you. If your xml changes to include further chars that need escaping, your home-made code will break
abaqasahAuthor Commented:
Thanks for the good suggestion. I will definitely use it to enhance my code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.