批量插入数据的优化
package com.atguigu.api.preparedstatement;
import org.junit.jupiter.api.Test;
import java.sql.*;
public class PSOtherPart {
/**
* 使用普通的方式插入10000数据
* @throws Exception
*/
@Test
public void testInsert() throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "1234");
//3.编写SQL语句结果,动态值的部分使用?代替
String sql="insert into t_user(account,password,nickname) values(?,?,?);";
//4.创建preparedStatement,并且传入SQL语句结果
PreparedStatement statement=connection.prepareStatement(sql);
long start = System.currentTimeMillis(); //存入开始的时间
for (int i = 0; i < 10000; i++) {
statement.setObject(1,"test"+i);
statement.setObject(2,"test"+i);
statement.setObject(3,"驴蛋蛋"+i);
statement.executeUpdate();
}
long end = System.currentTimeMillis(); //存结束的时间
// 结果解析
System.out.println("执行一万次数据插入消耗的时间 = "+(end-start));
//8.关闭资源
statement.close();
connection.close();
}
/**
* 使用批量插入的方式 插入一万条数据
* TODO:
* 1.路径后面添加??rewriteBatchedStatement=ture 允许批量插入
* 2.insert into values 【必须写】 语句不能添加;结束
* 3.不是执行语句每条,是批量添加 addBatch();
* 4.比哪里处添加完毕以后,统一批量执行executeBatch()
* @throws Exception
*/
@Test
public void testBatchInsert() throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu?rewriteBatchedStatement=ture", "root", "1234");
//3.编写SQL语句结果,动态值的部分使用?代替
String sql="insert into t_user(account,password,nickname) values(?,?,?)";
//4.创建preparedStatement,并且传入SQL语句结果
PreparedStatement statement=connection.prepareStatement(sql);
long start = System.currentTimeMillis();//存开始的时间
for (int i = 0; i < 10000; i++) {
statement.setObject(1,"test"+i);
statement.setObject(2,"test"+i);
statement.setObject(3,"驴蛋蛋"+i);
statement.addBatch(); // 不执行,追加到values的后面!
}
statement.executeBatch();//批量执行操作!
long end = System.currentTimeMillis(); //存结束的时间
// 结果解析
System.out.println("执行一万次数据插入消耗的时间 = "+(end-start));
//8.关闭资源
statement.close();
connection.close();
}
}
转账事务的实现
package com.atguigu.api.transaction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class BankDao {
/**
* 价钱的数据库操作方法 (jdbc)
*
* @param account 加钱的账号
* @param money 加钱的金额
* @param connection
*/
public void add(String account, int money, Connection connection) throws Exception {
//3.编写SQL语句结果,动态值的部分使用?代替
String sql="update t_bank set money=money + ? where account = ?;";
//4.创建preparedStatement,并且传入SQL语句结果
PreparedStatement statement = connection.prepareStatement(sql);
//5.占位符赋值
statement.setObject(1,money);
statement.setObject(2,account);
//6.发送SQL语句
int i = statement.executeUpdate();
//7.输出结果
//8.关闭资源
statement.close();
System.out.println("加钱成功! ");
}
/**
* 减钱的数据库操作方法 (jdbc)
* @param account 减钱的账户
* @param money 减钱的金额
*/
public void sub(String account,int money,Connection connection) throws Exception {
{
//3.编写SQL语句结果,动态值的部分使用?代替
String sql="update t_bank set money=money - ? where account = ?;";
//4.创建preparedStatement,并且传入SQL语句结果
PreparedStatement statement = connection.prepareStatement(sql);
//5.占位符赋值
statement.setObject(1,money);
statement.setObject(2,account);
//6.发送SQL语句
int i = statement.executeUpdate();
//7.输出结果
//8.关闭资源
statement.close();
System.out.println("减钱成功! ");
}
}
}
package com.atguigu.api.transaction;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.DriverManager;
public class BankService {
@Test
public void start() throws Exception {
//二狗子 给蛋蛋传五百
transfer("lvdandan","ergouzi",500);
}
/**
* TODO:
* 事务添加是在业务方法中!
* 利用try catch 代码块 ,开启 事务和提交事务,和 事务回滚!
* 将connecttion 传入dao层即可! dao只负责使用,不要close();
*
*
*
* @param addAcount
* @param subAccount
* @param money
* @throws Exception
*/
public void transfer(String addAcount, String subAccount,int money) throws Exception {
BankDao bankDao=new BankDao();
//一个事务的最基本要求,必须是同一个连接对象 connection、
//一个转账方法 属于一个十五(加钱 减钱)
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "1234");
try{
//开启事务
//关闭事务提交!
connection.setAutoCommit(false);
//执行数据库动作
bankDao.add(addAcount,money,connection);
System.out.println("--------------");
bankDao.sub(subAccount,money,connection);
//事务提交
connection.commit();
}catch (Exception e){
connection.rollback();
throw e;
}finally {
connection.close();
}
//
// bankDao.add(addAcount,money);
// System.out.println("----------------");
// bankDao.sub(subAccount,money);
}
}