Reputation: 77
I am trying to insert an email address into a MySQL using a java application. The problem I am having is that the "@" character is causing a MySQLSyntaxErrorException. I try to insert the email address as a String. How do i fix this?
Upvotes: 1
Views: 771
Reputation: 17864
String insert = "INSERT INTO customer_tbl(name, email) VALUES (?,?)";
PreparedStatement ps = con.prepareStatement(insert);
ps.setString(1,"name");
ps.setString(2,"[email protected]");
ps.executeUpdate();
A prepared statement let you use placemarkers, which can be set to anything and is not parsed by the SQL parser. As an added bonus, this makes you also immune for SQL injection.
Upvotes: 5
Reputation: 6499
Try to use PreparedStatement like in this example: http://www.exampledepot.com/egs/java.sql/InsertPs.html
Upvotes: 0
Reputation: 41767
Parameterize your query using a PreparedStatement. This way you do not have to worry about manual string escaping and are less vulnerable to SQL injection attacks.
Upvotes: 0