1

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.

1 Answers1

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