Java中做修改时如何根据主键获取本地数据
1个回答
展开全部
Oracle的啊 给你个例子,你自己改下表还有insert语句
public int save(User user) throws SQLException {// 插入数据
int id = 0;// 返回用户ID
String sql = "insert into regist_1 values(regist_1seq.nextval,?,?,?,?,?,?)";
String sql2 = "select regist_1seq.currval from dual";
//sql2就是获取序列自增的值,你把regist_1seq换成你自己的
Connection con = ConnectionUtils.openConnection();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getName());
stmt.setString(3, user.getPassword());
stmt.setInt(4, user.getAge());
stmt.setString(5, user.getSex());
stmt.setString(6, user.getTel());
stmt.executeUpdate();
stmt = con.prepareStatement(sql2);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
id = rs.getInt(1);
System.out.println("save id:" + id + "...");
}
ConnectionUtils.closeResult(rs);
ConnectionUtils.closeStatement(stmt);
ConnectionUtils.closeConnection(con);
return id;
}
public int save(User user) throws SQLException {// 插入数据
int id = 0;// 返回用户ID
String sql = "insert into regist_1 values(regist_1seq.nextval,?,?,?,?,?,?)";
String sql2 = "select regist_1seq.currval from dual";
//sql2就是获取序列自增的值,你把regist_1seq换成你自己的
Connection con = ConnectionUtils.openConnection();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getName());
stmt.setString(3, user.getPassword());
stmt.setInt(4, user.getAge());
stmt.setString(5, user.getSex());
stmt.setString(6, user.getTel());
stmt.executeUpdate();
stmt = con.prepareStatement(sql2);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
id = rs.getInt(1);
System.out.println("save id:" + id + "...");
}
ConnectionUtils.closeResult(rs);
ConnectionUtils.closeStatement(stmt);
ConnectionUtils.closeConnection(con);
return id;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询