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?
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?
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.