获得数据库自动生成的主键
示例:
packagecom.hbsi.test;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importcom.hbsi.utils.DBManager;
public class Demo4{
/**
*@param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=null;
PreparedStatement st=null;
ResultSet rs=null;
try {
con=DBManager.getConnection();
String sql="insert intousers (name,password) values (?,?)";
st=con.prepareStatement(sql);
st.setString(1,"Jerry");
st.setString(2,"QAZ");
st.executeUpdate();
rs=st.getGeneratedKeys();//结果集单行单列
if(rs.next()){
System.out.println(rs.getInt(1));
}
} catch (SQLException e) {
// TODO Auto-generated catchblock
e.printStackTrace();
} finally{
&
相关新闻>>
- 发表评论
-
- 最新评论 更多>>