2012-05-20 97 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给出了一个很好的例子。看看它。