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
);
看看你的update最后组成的sql语句是怎么样的,然后放到Mysql里面去执行看看,就知道问题在什么地方了。
now(), 这样用也 可以?