Java 获取Mysql插入的自增ID

String sql = "insert into discuz_forum_forum (fup, type, name, status, displayorder) "
		+ "values (?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql,
		Statement.RETURN_GENERATED_KEYS);

ps.setInt(1, forum.getInt("fup"));
ps.setString(2, forum.getString("type"));
ps.setString(3, forum.getString("name"));
ps.setInt(4, forum.getInt("status"));
ps.setInt(5, forum.getInt("displayorder"));

ps.esxecute();

int generatedKey = -1;
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
	generatedKey = rs.getInt(1);
}

return generatedKey;

 

发表评论