Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Problems with apostrophes in SQL

  Asked By: Vernon    Date: Jul 07    Category: Java    Views: 1051
  

What I thought was a minor problem has become a bear: apostrophe in
sql statemements. I read that the thing to do is use a
preparedStatement. Here's my code:
String query = "INSERT INTO names (first, last, idNum) VALUES
(?, ?, ?) ";

System.out.println(query);

try {
PreparedStatement pSt = cn.prepareStatement(query);
pSt.setString(1,first);
pSt.setString(2,last);
pSt.setInt(3,1);
}
pSt.executeUpdate(query);
pSt.close();

Everything works fine until I input a name with an apostrophe, then
it throws an SQL Syntax error. I've tried many other things
without successful. I'm running sdk 1.4.1, mysql and J/Connector.
Any suggestions would be appreciated.

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Problems with apostrophes in SQL Or get search suggestion and latest updates.




Tagged: