Java中last_insert_id的使用

來源:互聯網
上載者:User

標籤:sql   tsql   ast   spring   主鍵   方式   --   cto   import   

last_insert_id的作用是:在當前表中的主鍵是自增時,插入一條新記錄的同時使用last_insert_id可以擷取當前的新記錄的主鍵id。

下面是一個例子:

import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.PreparedStatementCreator;import org.springframework.jdbc.support.GeneratedKeyHolder;import org.springframework.jdbc.support.KeyHolder;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:spring.xml","classpath:applicationContext.xml"})public class TestSql {    @Autowired(required = true)     private JdbcTemplate jdbcTemplate;     @Autowired(required = true)     private JdbcTemplate jdbcTemplate1;         @Test    public void select(){        final String sql = "insert into series(class_id,brand_id,series_name) values("+2+","+2+",‘"+ "sfsfds‘)";        System.out.println(sql);                /*        //這種方式會出現主鍵不確定性(當在另一張表同時插入一條資料並也要擷取主鍵時會出現重複或者為0的情況)        int ret = jdbcTemplate.update(sql);        System.out.println("系列傳回值==="+ret);        String last = "select last_insert_id()";        int lastId = jdbcTemplate.queryForInt(last);        System.out.println("最新系列ID===="+lastId);        */                KeyHolder keyHolder = new GeneratedKeyHolder();          //這種方式是在上一種方式之上最佳化其不確定性(避免出現上述重複或者為0的情況)        int i = jdbcTemplate.update(new PreparedStatementCreator() {              public PreparedStatement createPreparedStatement(Connection con) throws SQLException {                  return con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);                //or return con.prepareStatement(sql, new String[]{"ID"});//ID是自動成長的自動              }          }, keyHolder);          System.out.println(i);        System.out.println(keyHolder.getKeyList());                final String sql1 = "insert into meta_type(series_id,type_name) values("+2+",‘"+ "sfsfds‘)";        System.out.println(sql1);//        KeyHolder keyHolder1 = new GeneratedKeyHolder();          int j = jdbcTemplate1.update(new PreparedStatementCreator() {              public PreparedStatement createPreparedStatement(Connection con) throws SQLException {                  return con.prepareStatement(sql1, PreparedStatement.RETURN_GENERATED_KEYS);                  //or return con.prepareStatement(sql, new String[]{"ID"});//ID是自動成長的自動              }          }, keyHolder);          System.out.println("----"+j);          System.out.println(keyHolder.getKeyList());    }    }

 

Java中last_insert_id的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.