The test script is as follows:
1 CREATE TABLE test12 (3 ID int primary KEY auto_increment,4 name varchar (20) 5);
Test code:
1 package Me.gacl.demo; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import Me.gacl.ut Ils. Jdbcutils; 7 8 public class Test {9 public static void Main (string[] args) { Connection conn = null;11 Preparedsta Tement st = null;12 ResultSet rs = null;13 try{14 conn = Jdbcutils.getconnection (); String sql = "Inser T into test1 (name) VALUES (?) "; + st = conn.preparestatement (SQL), st.setstring (1, "AAA"), st.executeupdate (); Gets the database auto-generated primary key rs = St.getgeneratedkeys (); if (Rs.next ()) {System.out.println ( rs.getint (1)); 23 }24 }catch (Exception e) { e.printstacktrace ();}finally{27 jdbcutils.release (conn, St, RS); }29 }30}
Javaweb Learning Summary (37)--Get the primary key automatically generated by MySQL database