java使用PreparedStatement插入數據到mysql並返回id 的源代碼
public int add(Video vd) throws Exception {
String sql="insert into msg_video_data(name,`rename`,title,content,date,src) values(?,?,?,?,?,?)";
int id=0;
try{
conn=getConns();
ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.setString(1, vd.getName());
ps.setString(2, vd.getRename());
ps.setString(3, vd.getTitle());
ps.setString(4, vd.getContent());
ps.setString(5, vd.getDate());
ps.setString(6,vd.getSrc());
ps.executeUpdate();
rs=ps.getGeneratedKeys();
rs.next();
id=rs.getInt(1);
}catch(Exception e){
throw e;
}finally{
close();
}
return id;