//取得数据库自动生成的主键
Connection connection=null;
PreparedStatement preparedStatement =null;
try {
connection=JdbcTool.getConnection();
String sql="insert into customers(name,email,birth)" +"values(?,?,?)";
//preparedStatement=connection.prepareStatement(sql);
//使用重载的prepareStatement(sql,flag)来生成prepareStatement对象
preparedStatement=connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1,"ABCDE");
preparedStatement.setString(2,"abc.com");
preparedStatement.setDate(3,new Date(new java.util.Date().getTime()));
preparedStatement.executeUpdate();
//通过getGeneratedKeys()获取了包含新生成的主键的ResultSet对象
ResultSet rs =preparedStatement.getGeneratedKeys();
if(rs.next()){
System.out.println(rs.getObject(1));
}
ResultSetMetaData rsmd =rs.getMetaData();
for (int i=0;i<rsmd.getColumnCount();i++){
System.out.println(rsmd.getColumnName(i+1));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcTool.release(null,preparedStatement,connection);
}