GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - dunkcoder/insertfetch: retrieve auto-generated key

BranchesTags
Open more actions menu

Latest commit

 

History

7 Commits
7 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
README.md
README.md
 
 
pom.xml
pom.xml
 
 

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

Readme
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.