JAva挿入データの自己成長idの取得
1292 ワード
/**
*
* , ( id)
*
* @author LZL
*
*/
public class Auto_Increment {
private static Connection conn = null;
private static PreparedStatement stsm = null;
private static ResultSet rs = null;
@Test
public void testGetAutoIncrement() {
try {
// 1:
conn = Jdbcutil.getConnection();
// 2: sql
String sql = "INSERT INTO person (NAME,sex,age) VALUES (?,?,?);";
// 3: sql ( )
stsm = conn.prepareStatement(sql,
PreparedStatement.RETURN_GENERATED_KEYS);
// 4:
stsm.setString(1, " ");
stsm.setString(2, " ");
stsm.setInt(3, 22);
// 5: , sql
stsm.executeUpdate();
// 6: ,
rs = stsm.getGeneratedKeys();
// 7:
if (rs.next()) {
System.out.println(" :" + rs.getInt(1));
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
}
Jdbcutil.close(conn, stsm, rs);
}
}