请教大神,Java insert插入数据,怎么返回ID的值
publicstaticStringinsertData(Useruser){Stringid="";//先调用getConnectionif(getConnection...
public static String insertData(User user){
String id = "" ;
//先调用getConnection
if(getConnection()){
try {
String sql = "insert into t_user(id,username,password,email,sex)values(TJ_USER.Nextval,?,?,?,?)";
//创建prepareStatement对象
pst = cn.prepareStatement(sql);
//设置参数值
pst.setString(1, user.getUserName());
pst.setString(2, user.getPassWord());
pst.setString(3, user.getEmail());
pst.setString(4, user.getSex());
//if(pst.execute()){
//执行
pst.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}
}
return id;
}
我想返回ID的值,怎么写啊,我都纠结了一天了,怎么都返回不了ID的值,插入完,返回ID的值 展开
String id = "" ;
//先调用getConnection
if(getConnection()){
try {
String sql = "insert into t_user(id,username,password,email,sex)values(TJ_USER.Nextval,?,?,?,?)";
//创建prepareStatement对象
pst = cn.prepareStatement(sql);
//设置参数值
pst.setString(1, user.getUserName());
pst.setString(2, user.getPassWord());
pst.setString(3, user.getEmail());
pst.setString(4, user.getSex());
//if(pst.execute()){
//执行
pst.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}
}
return id;
}
我想返回ID的值,怎么写啊,我都纠结了一天了,怎么都返回不了ID的值,插入完,返回ID的值 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询