I have a database with a "username" field and with Java I insert the values into the database. I tried to enter the username "l'ornitorinco" but the program does not work (crashes). I know that when I insert an apostrophe string with phpMyAdmin like "l'ornitorinco" it is sent in this way "l'ornitorinco". I should probably analyze the string and insert the apostrophe at the appropriate point (with a for loop from 0 to string.length), but does anyone know a more appropriate method? I had read about a method to set the strings for mysql use, but I not remember.
Asked
Active
Viewed 402 times
1
-
will escape character \ help? – Boyke Ferdinandes Jul 13 '20 at 22:22
-
@BoykeFerdinandes I don't understand what you mean – gianlucavallo Jul 14 '20 at 05:43
-
it was something like this `https://stackoverflow.com/questions/3844595/how-can-i-make-java-print-quotes-like-hello#:~:text=print(%22%5C%22Hello%5C,r%22%20and%20%22%5Cn%22` but you have your answer so all good~ – Boyke Ferdinandes Jul 14 '20 at 22:31
1 Answers
3
You could use a PreparedStatement
, as its setString()
method allows you to include apostrophes on the value, making it much simpler than playing the single/doble quote game.
String queryString = "insert into username values (?)";
query = con.prepareStatement(queryString);
query.setString(1, "l'ornitorinco");
query.executeUpdate();
Take a look here for more info. Hope it helps!

aran
- 10,978
- 5
- 39
- 69