JDBC——取得数据库自动生成的主键

Connection conn = JdbcUtil.getConnection();

String sql = "insert into user(name,password,email,birthday) 
			values('abc','123','abc@sina.com','1978-08-08')";
PreparedStatement st = conn.
			prepareStatement(sql,Statement.RETURN_GENERATED_KEYS );

st.executeUpdate();
ResultSet rs = st.getGeneratedKeys();  //得到插入行的主键
if(rs.next())
	System.out.println(rs.getObject(1));

  

posted @ 2017-11-20 16:40  SkyeAngel  阅读(255)  评论(0编辑  收藏  举报