Skip to content

dunkcoder/insertfetch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Jdbc PreparedStatement way

  • Step1: Prepare RETURN_GENERATED_KEYS statement using insertSql
String insertSql = "insert into t_user (username) values('zhangsan')";
PreparedStatement pstmt = conn.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);
  • Step2: Execute insertSql using statement
pstmt.executeUpdate();
  • Step3: Retrieve auto-generated keys
ResultSet rs = pstmt.getGeneratedKeys();
Assert.assertTrue(rs.getLong(1) > 0);

Spring JdbcTemplate way

final String insertSql = "insert into t_user (username) values(?)";
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
    public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
        PreparedStatement ps = connection.prepareStatement(insertSql, new String[] { "id" });
        ps.setString(1, 'zhangsan');
        return ps;
	}
}, keyHolder);
Assert.assertTrue(keyHolder.getKey().longValue() > 0);
For run JUnit Test a greenbar, config db parameters on below places:
JdbcUtilTest.java setUpBeforeClass method
application-beans.xml dataSource bean

About

retrieve auto-generated key

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages