JDBC学习笔记第五篇

package com.shayiheng.api.preparedstatement;

import org.junit.Test;

import java.sql.*;

/**
 * 5
 * @Author Tom
 * Description: 练习ps的特殊使用情况
 */
public class PSOtherPart {
    /**
     * TODO:
     *      t_user插入一条数据!并且获取数据库自增长的主键!
     * TODO:使用总结
     *      1.创建prepareStatement的时候,告知携带会数据库自增长的主键(sql,Statement.RETURN_GENERATED_KEYS);
     *      2.获取装主键的结果集对象,一行一列,获取相应的数据即可   ResultSet resultSet=statement.getGeneratedKeys();
     */
    @Test
    public void  returnPrimarykey() throws ClassNotFoundException, SQLException {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/shayiheng?user=root&password=12345678");
        //3.编写SQL语句结果
        String sql="insert into t_user(account,password,nickname)value(?,?,?);";
        //4.创建预编译preparedStatement
        //Statement.RETURN_GENERATED_KEYS 去寻找数据库自增长的主键
        PreparedStatement statement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
        //5.占位符赋值
        statement.setObject(1,"test5");
        statement.setObject(2,"123456");
        statement.setObject(3,"小狗狗");
        //6.发送SQL语句
        int i= statement.executeUpdate();
        //7.结果解析
        if(i>0) {
            System.out.println("插入成功");

            //可以获取回显的主键
            //获取主键的结果集对象, 一行 一列 id=值
            ResultSet resultSet=statement.getGeneratedKeys();
            resultSet.next();//移动下光标!
            int id=resultSet.getInt(1);//一行一列
            System.out.println("id="+ id);
        }
        else {
            System.out.println("插入失败");
        }
        //8.关闭资源
        statement.close();
        connection.close();

    }

    /**
     * 使用普通的方式插入10000条数据
     * @throws SQLException
     */
    @Test
    public void  testInsert() throws ClassNotFoundException, SQLException {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/shayiheng?user=root&password=12345678");
        //3.编写SQL语句结果
        String sql="insert into t_user(account,password,nickname)value(?,?,?);";
        //4.创建预编译preparedStatement
        //Statement.RETURN_GENERATED_KEYS 去寻找数据库自增长的主键
        PreparedStatement statement = connection.prepareStatement(sql);

        long start = System.currentTimeMillis();
        //5.占位符赋值
        for (int i = 0; i < 10000; i++) {
            statement.setObject(1,"dd"+i);
            statement.setObject(2,"dd"+i);
            statement.setObject(3,"小狗狗");
            //6.发送SQL语句,并且获取结果
            statement.executeUpdate();
        }
        long end = System.currentTimeMillis();
        //7.结果解析
        System.out.println("执行10000次数据插入消耗的时间:"+(end-start));
        //8.关闭资源
        statement.close();
        connection.close();
        }


    /**
     * 使用批量插入的方式插入10000条数据
     * TODO:总结批量插入
     *      1.路径后面添加 ?rewriteBatchedStatements=true 允许批量插入
     *      2.insert into values  符合SQL语法 语句不难添加;结束
     *      3.不是执行语句每条,是批量添加addBatch();
     *      4.遍历添加完毕以后,统一批量执行
     */
    @Test
    public void  testBatchInsert() throws ClassNotFoundException, SQLException {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接 想要支持批量插入要在路径上加 ?rewriteBatchedStatements=true 把value改成values
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/shayiheng?rewriteBatchedStatements=true","root","12345678");
        //3.编写SQL语句结果
        String sql="insert into t_user(account,password,nickname)values(?,?,?)";
        //4.创建预编译preparedStatement
        //Statement.RETURN_GENERATED_KEYS 去寻找数据库自增长的主键
        PreparedStatement statement = connection.prepareStatement(sql);

        long start = System.currentTimeMillis();
        //5.占位符赋值
        for (int i = 10000; i <=20000; i++) {
            statement.setObject(1,"dd"+i);
            statement.setObject(2,"dd"+i);
            statement.setObject(3,"小狗狗");
            //6.发送SQL语句,并且获取结果
            statement.addBatch();//不执行,追加到values的后面
        }
        statement.executeBatch();//批量操作

        long end = System.currentTimeMillis();
        //7.结果解析
        System.out.println("执行10000次数据插入消耗的时间:"+(end-start));
        //8.关闭资源
        statement.close();
        connection.close();
    }
}

你可能感兴趣的:(JDBC,学习,笔记)