2013-03-09 18 views
1

我正在檢索YouTube視頻上的評論。到目前爲止,我成功地能夠讀取和解析XML文件。問題是我必須創建一個評論和視頻鏈接數據庫。我使用下面的查詢中的java:在MySql表中插入特定類型的文本時遇到問題

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

的問題是像下面存在於評論不能插入文本:

「嘿,這,這是‘非常’酷」或者「嘿我正在尋找解決方案「

由於\"\'引號。我不想刪除它們,因爲我必須做一些語言學處理,我不想改變它的含義(我是 - >我對我來說是不同的)。我如何插入它們?我正在使用java來處理所有這些。

回答

2

使用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幫助您從SQL Injection避免。

相關問題