I'm still somewhat new to Java and trying to insert data into a database. I'm getting an error when inserting a string containing 's so my end result would be to escape the apostrophe. How can I accomplish?
Use prepared statements. As well as handling any special characters, they are much more robust and help prevent sql injection attacks.
The issue really isn't with Java, rather with the underlying database. Most likely you are stringing your parameters together like this:
String sql = "select * from sometable where somefield = " + someObject.getSomeField();
Don't do that. Use PreparedStatement's instead.
That has the added advantage of preventing SQL injection attacks, if this is an application that has to be concerned about such things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With