Obtained from the database by the primary key (F)

Often use Hibernate, mybatis time when an inserted data, the original empty main key of the entity on Bean assign a new value. In JDBC, you can also do this by a disguised way.

Use prepareStatement (String sql, int autoGeneratedKeys) to define the PreparedStatement object. And specify: PreparedStatement.RETURN_GENERATED_KEYS.

PreparedStatement psmt = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
psmt.executeUpdate();
ResultSet keyResultSet = psmt.getGeneratedKeys();
keyResultSet.next();
long num =keyResultSet.getLong(1);

 

Guess you like

Origin www.cnblogs.com/myitnews/p/11871221.html