首页 新闻 会员 周边

下面的代码为什么会出现 SQLException ?

0
[已解决问题] 解决于 2009-10-28 14:43

String sql = "insert into article values (null, 0, ?, ?, ?, now(), 0)";
 PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
 Statement stmt = conn.createStatement();
 
 pstmt.setInt(1, -1);
 pstmt.setString(2, title);
 pstmt.setString(3, cont);
 pstmt.executeUpdate();
 
 ResultSet rsKey = pstmt.getGeneratedKeys();
 rsKey.next();
 int key = rsKey.getInt(1);
 rsKey.close();
 stmt.executeUpdate("update article set rootid = " + key + " where id = " + key);

 

抛出的异常为:java.sql.SQLException: 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 'id = 11' at line 1

用的是MySQL数据库,数据库中的表是这样建立的:

create table article (
id int primary key auto_increment,
pid int,
rootid int,
title varchar(255),
cont text,
pdate datetime,
isleaf int
);

Jinging的主页 Jinging | 初学一级 | 园豆:200
提问于:2009-10-28 10:06
< >
分享
最佳答案
0

看看你的update最后组成的sql语句是怎么样的,然后放到Mysql里面去执行看看,就知道问题在什么地方了。

清海扬波 | 小虾三级 |园豆:825 | 2009-10-28 11:44
根据你的建议,问题解决了:update 最后组成的sql语句中缺少一个空格。
Jinging | 园豆:200 (初学一级) | 2009-10-28 14:42
其他回答(1)
0

now(), 这样用也 可以?

天堂口 | 园豆:514 (小虾三级) | 2009-10-28 10:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册