MySQL的批处理
来源:互联网 发布:无创dna数据辨别男女 编辑:程序博客网 时间:2024/04/27 01:18
批处理,降低与数据库的连接次数,提高执行效率。
BatchDemo.java文件:
package com.itheima.jdbc;import java.sql.Connection;import java.sql.Statement;import org.junit.Test;import com.itheima.util.JdbcUtil;/** * 批处理,降低与数据库的连接次数,提高执行效率 * @author wym * */public class BatchDemo { /* create database day16; use day16; create table t1( id int primary key, name varchar(100) ); */ //向数据库中插入2条记录,再把第1条删掉 @Test public void test1() throws Exception{ Connection conn = JdbcUtil.getConnection(); Statement stmt = conn.createStatement(); String sql1 = "insert into t1 values(1,'aaa')"; String sql2 = "insert into t1 values(2,'bbb')"; String sql3 = "delete from t1 where id=1"; stmt.addBatch(sql1);//Statement实例内部有一个List,sql语句加到List中了 stmt.addBatch(sql2); stmt.addBatch(sql3); int[] ii = stmt.executeBatch();//返回的是一个数组,数组的元素为每条语句影响到的行数 for(int i : ii){ System.out.println(i); } JdbcUtil.release(null, stmt, conn); } //插入10条记录 @Test public void test2() throws Exception{ Connection conn = JdbcUtil.getConnection(); java.sql.PreparedStatement stmt = conn.prepareStatement("insert into t1 values(?,?)"); for(int i=0; i<10; i++){ stmt.setInt(1, i+1); stmt.setString(2,"aaaa"+(i+1)); stmt.addBatch(); } stmt.executeBatch();//返回的是一个数组,数组的元素为每条语句影响到的行数 JdbcUtil.release(null, stmt, conn); } //插入10000条数据 @Test public void test3() throws Exception{ Connection conn = JdbcUtil.getConnection(); java.sql.PreparedStatement stmt = conn.prepareStatement("insert into t1 values(?,?)"); for(int i=0; i<10000; i++){ stmt.setInt(1, i+1); stmt.setString(2,"aaaa"+(i+1)); stmt.addBatch(); if(i%1000 ==0){ stmt.executeBatch(); stmt.clearBatch(); } } stmt.executeBatch();//返回的是一个数组,数组的元素为每条语句影响到的行数 JdbcUtil.release(null, stmt, conn); }}
JdbcUtil.java文件:
package com.itheima.util;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;//工具类public class JdbcUtil { private static String driverClass; private static String url; private static String user; private static String password; static{ try { ClassLoader cl = JdbcUtil.class.getClassLoader(); InputStream in = cl.getResourceAsStream("dbcfg.properties"); Properties props = new Properties(); props.load(in); driverClass = props.getProperty("driverClass"); url = props.getProperty("url"); user = props.getProperty("user"); password = props.getProperty("password"); Class.forName(driverClass); } catch (Exception e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws Exception{ Connection conn = DriverManager.getConnection(url,user, password); return conn; } public static void release(ResultSet rs,Statement stmt,Connection conn){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } }}
dbcnfg.properties文件:
driverClass=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/day16user=rootpassword=admin
运行结果:
1 aaaa1
2 aaaa2
3 aaaa3
4 aaaa4
5 aaaa5
。。。共10000条
1 0
- MySQL的批处理启动
- MySQL的批处理
- windows下的mysql批处理
- MYSQL批处理
- mysql批处理
- MySQL批处理
- MySQL插入数据库的批处理命令
- jdbc实现对mysql数据库的批处理
- 批处理 简单实现mysql 的 window的自动备份脚本
- 快速打开mysql的方法,简单的批处理命令
- mysql 批处理命令备份
- 批处理操作mysql数据库
- mysql 备份批处理
- MySQL批处理SQL语句
- mysql批处理备份
- java语言MySQL批处理
- bat批处理执行MySQL
- mysql批处理性能优化
- android javascript 调用activity
- 使用 Swift 进行 JSON 解析
- 菜菜鸟学习Vue.js
- 5-TCP Client&Server Example
- RGB 转 edp 接口dp501 示范电路
- MySQL的批处理
- PHP XML和数组互相转换
- 6-I&O Multiplexing-The 'select' and 'poll' Functions
- java中的权限修饰符
- SimpleJson解析及调用
- HHU 1001 汽水瓶(模拟)
- JavaEE项目实战(OA系统)之九_部门管理之三
- MySQL学习笔记六
- bash shell中expr命令下几种的使用