Jaspreet Singh
Jaspreet Singh

Reputation: 77

Insert an email address into MySQL using a java application

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

Answers (3)

GeertPt
GeertPt

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

dbf
dbf

Reputation: 6499

Try to use PreparedStatement like in this example: http://www.exampledepot.com/egs/java.sql/InsertPs.html

Upvotes: 0

Rich O'Kelly
Rich O'Kelly

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

Related Questions