MaxSteel
MaxSteel

Reputation: 259

Trouble in inserting particular type of text in MySql table

I am working on retrieving comments on youtube video. So far I am successfully able to read and parse the XML files. The problem is I have to create a db of comments and videos links. I am using following query in java:

sql = "insert into comments_db(`video_link`,`comment`) values(\""
                        + ura[k] + "\",\"" + comment + "\")";

The problem is text like following present in the comments cannot be inserted:

"hey this this is "very" cool" or "Hey I'm looking for solution"

Because of the \" or \' quotes. I do not want to remove them since I have to do some linguistics processing and I do not want to change the meaning (I'm->Im is different for me). How can I insert them?I am using java for all these.

Upvotes: 1

Views: 97

Answers (1)

John Woo
John Woo

Reputation: 263733

use PreparedStatement, ex,

String query = "insert into comments_db(video_link, comment) VALUES (?, ?)";
PreparedStatement insertQuery = con.prepareStatement(query); 
insertQuery.setString(1, "He said, \"I'm here!\"");
insertQuery.setString(2, "I'm Here");
insertQuery.executeUpdate();

PreparedStatement helps you avoid from SQL Injection.

Upvotes: 2

Related Questions