I’m using Java 1.7 to make an aplication how conects to a MySQL DB.But I Have many problems into que insertion of values with the special characters like “,\,\n.
I’m Trying to change manually this values.
Im using this method to make the change.
public static String StringToSQL(String chain) {
chain.replaceAll(Matcher.quoteReplacement("\\"),
Matcher.quoteReplacement("\\\\"));
chain.replaceAll(Matcher.quoteReplacement("\""),
Matcher.quoteReplacement("\\\""));
chain.replaceAll(Matcher.quoteReplacement("\n"),
Matcher.quoteReplacement("\\n"));
chain.replaceAll(Matcher.quoteReplacement("'"),
Matcher.quoteReplacement("\\'"));
return chain;
}
I wanna try to transform a chain like
The cat says "meaw meaw"
into
the cat says \"meaw meaw\"
to haven’t problems with the MySql Syntax
for example the sql must be
INSERT INTO AnimalSays (Phrase) VALUES ("\"Meaw Meaw\"");
but I send
INSERT INTO AnimalSays (Phrase) VALUES (""Meaw Meaw"");
Having a syntax error how breaks the application
Using a prepared statement will solve your problem. It will escape all special characters if you set the string parameter.
http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html