2012-05-20 128 views
0

我想插入一些數據到MySQL數據庫,但我得到一個錯誤,我找不到錯誤的代碼。我不擅長mysql。JDBC MySQL查詢錯誤?

String insertQuery = "insert into books(title, author, description, prize)values(" 
      + title 
      + "," 
      + author 
      + "," 
      + desc 
      + ", " 
      + prize 
      + ");"; 

mysql> describe books; 
+-------------+--------------+------+-----+---------+-------+ 
| Field  | Type   | Null | Key | Default | Extra | 
+-------------+--------------+------+-----+---------+-------+ 
| book_id  | int(11)  | NO | PRI | NULL |  | 
| title  | varchar(64) | YES |  | NULL |  | 
| author  | varchar(64) | YES |  | NULL |  | 
| description | varchar(500) | YES |  | NULL |  | 
| prize  | float  | YES |  | NULL |  | 
| added  | datetime  | YES |  | NULL |  | 
+-------------+--------------+------+-----+---------+-------+ 
6 rows in set (0.01 sec) 

我得到的是錯誤,

Query: insert into books(title, author, description, prize)values(sfdfdf,fdfdf,Please limiasasaat your response to 500 characters., 78.9); 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limiasasaat your response to 500 characters., 78.9)' at line 1 

回答

4

您的字符串值不與單引號括起來。試試這個:

String insertQuery = "insert into books(title, author, description, prize)values('" 
      + title 
      + "','" 
      + author 
      + "','" 
      + desc 
      + "', " 
      + prize 
      + ");"; 

Example of Prepared Statement at RoseIndia

UPDATE的PreparedStatement的1

樣本:

//other codes here 
String iSQL = "Insert into books(title, author, description, prize) values (?,?,?,?)"; 
// con is your active connection object 
PreparedStatement pstmt = con.prepareStatement(iSQL); 
pstmt.setString(1, title); 
pstmt.setString(2, author); 
pstmt.setString(3, desc); 
pstmt.setFloat(4, prize); 
pstmt.executeUpdate(); 
//other codes here 

,但在你的班上名列前茅不要忘了這個說法:import java.sql.*;

+5

請避免鏈接到RoseIndia。 http://balusc.blogspot.in/2008/06/what-is-it-with-roseindia.html。 –

+0

我添加了參考bro的鏈接,這就是爲什麼我根據OP的問題更新了準備好的語句的答案。例如, –

+0

+1。 –

3

您的問題是,你不必在你的varchar參數,我認爲行情。

String insertQuery = "insert into books(title, author, description, prize)values('" 
      + title 
      + "','" 
      + author 
      + "','" 
      + desc 
      + "', " 
      + prize 
      + ");"; 

然而,這仍然不是一個很好的解決方案,你仍然無法處理其中包含一個任意輸入:

,你可以通過簡單地包繞着那些需要這些PARAMATERS引號解決這個問題報價(如果desc參數包含字符串Don't try this查詢將然後失敗,因爲在don't'將終止desc參數,然後解析器會期望一個,但會遇到t try this),並公開向SQL injection attack

你應該做的是使用準備好的語句。這使得它很安全,你不需要擔心轉義問題,因爲這一切都是爲你處理的。你可以按照上面的指示來破解它。但是爲了您自己的理智,並且爲了所有需要處理您的代碼的人,請花一點時間學習準備好的語句並使用它們。未來你會感謝當前你。

有一個prepared statement tutorial on the oracle java website

使用基於索引的預準備語句總是對我來說似乎有點脆弱。有一個example of how you can bind parameters by name它可以讓你的PreparedStatement一點更容易閱讀,所以他們最終可能會像這樣:

String iSQL = "Insert into books(title, author, description, prize) values (:title,:author,:description,:prize)"; 

NamedParameterStatement pstmt =new NamedParameterStatement(connection,iSQL); 
pstmt.setString("title", title); 
pstmt.setString("author", author); 
pstmt.setString("description", desc); 
pstmt.setFloat("prize", prize); 
pstmt.executeUpdate(); 
+1

他應該逃避他們或使用準備好的語句。 – Corbin

+0

@Corbin絕對,當你發佈時只是充實了我的答案:) –

1

我建議使用Prepared Statement,但是如果你仍然想用你的方式去做,那麼你將不得不以某種方式告訴MYSQL這是一個字符條目,這是一個整數條目。

要定義字符條目和整數條目之間的差異,您必須在字符條目周圍添加單引號。

1

使用preparedStatement。在價值觀念發生變化的地方使用陳述肯定會讓你得出這樣的結論。 johntotetwoo給出了一個很好的例子。看看它。