zoukankan      html  css  js  c++  java
  • 五.获得MYSQL数据库自动生成的主键

    测试脚本如下:

    1  create table test1
    2 (
    3      id int primary key auto_increment,
    4      name varchar(20)
    5 );

    测试代码:

    package me.tanlei.demo;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import me.tanlei.jdbc.jdbcUtils;
    
    public class Test {
       public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement pStatement=null;
        ResultSet resultSet=null;
        
        try {
            connection=jdbcUtils.getConnection();
           String sql="insert into test1(name) values(?)";
           pStatement=connection.prepareStatement(sql);
           pStatement.setString(1, "aaa");
           pStatement.executeUpdate();
           //获取数据库自动生成的主键
           resultSet=pStatement.getGeneratedKeys();
           if(resultSet.next()) {
               System.out.println(resultSet.getInt(1));
           }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            jdbcUtils.release(connection, pStatement, resultSet);
        }
        
    }
    }
  • 相关阅读:
    Go--指针
    Go--struct
    Go--函数
    Go基础
    流程控制
    Go前言
    变量与常量
    Django(三):HttpRequest和HttpResponse
    Django(二):url和views
    tensorflow(一):图片处理
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/9505186.html
Copyright © 2011-2022 走看看