zoukankan      html  css  js  c++  java
  • JDBC中级篇——批处理和PreparedStatement对有sql缓冲区的数据库的友好,测试

    微笑注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接:

    package a_batch;
    
    import util.JdbcUtil;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    /**
     * 同时插入2000条数据
     *
     *	对于JDBC中的批处理的测试!
     *
     * 结论:
     * 		1) mysql数据库不支持PreparedStatement优化,而且不支持批处理优化。
     * 		2) oracle数据库即支持PreparedStatement优化,也支持批处理优化。   
     * @author mzy
     */
    public class Demo01 {
    	
    	public static void main(String[] args) {
    		//testByStaement();
    		//testByStaementBatch();
    		//testByPreparedStaement();
    		//testByPreparedStaementBatch();
    		
    		testTime();
    	}
    	
    	/**
    	 * 测试执行速度
    	 */
    	public static void testTime(){
    		long start = System.currentTimeMillis();
    		//testByStaement();
    		//testByStaementBatch();
    		//testByPreparedStaement();
    		testByPreparedStaementBatch();
    		long end = System.currentTimeMillis();
    		System.out.println("耗时为:"+(end-start));
    	}
    
    	/**
    	 * 没有批处理的Statement的情况
    	 * mysql: 耗时为:7838   oracle:耗时为:6580
    	 */
    	public static void testByStaement(){
    		Connection conn = null;
    		Statement stmt = null;
    		try{
    			conn = JdbcUtil.getConnection();
    			stmt = conn.createStatement();
    			for(int i=1;i<=2000;i++){
    				stmt.executeUpdate("INSERT INTO student VALUES("+i+",'张三',20,'男')");
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			JdbcUtil.close(stmt, conn);
    		}
    	}
    	
    	
    	/**
    	 * 使用批处理的Statement的情况
    	 * mysql: 耗时为:9097  oracle:耗时为:5477
    	 */
    	public static void testByStaementBatch(){
    		Connection conn = null;
    		Statement stmt = null;
    		try{
    			conn = JdbcUtil.getConnection();
    			stmt = conn.createStatement();
    			for(int i=1;i<=2000;i++){
    				//把sql添加到缓存区
    				stmt.addBatch("INSERT INTO student VALUES("+i+",'张三',20,'男')");
    				//每20条发送sql
    				if(i%20==0){
    					//执行批处理命令
    					stmt.executeBatch();
    					//清空缓存区
    					stmt.clearBatch();
    				}
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			JdbcUtil.close(stmt, conn);
    		}
    	}
    	
    	/**
    	 * 没有批处理的PrepaedStatement的情况
    	 * mysql: 耗时为:9051 oracle:耗时为:4161
    	 */
    	public static void testByPreparedStaement(){
    		Connection conn = null;
    		PreparedStatement stmt = null;
    		try{
    			conn = JdbcUtil.getConnection();
    			stmt = conn.prepareStatement("INSERT INTO student VALUES(?,?,?,?)");
    			for(int i=1;i<=2000;i++){
    				//参数赋值
    				stmt.setInt(1, i);
    				stmt.setString(2, "张三");
    				stmt.setInt(3, 20);
    				stmt.setString(4, "男");
    				//执行
    				stmt.executeUpdate();
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			JdbcUtil.close(stmt, conn);
    		}
    	}
    	
    	
    	/**
    	 * 使用批处理的PrepaedStatement的情况
    	 * mysql:耗时为:9379 oracle: 耗时为:1391
    	 */
    	public static void testByPreparedStaementBatch(){
    		Connection conn = null;
    		PreparedStatement stmt = null;
    		try{
    			conn = JdbcUtil.getConnection();
    			stmt = conn.prepareStatement("INSERT INTO student VALUES(?,?,?,?)");
    			for(int i=1;i<=2000;i++){
    				//参数赋值
    				stmt.setInt(1, i);
    				stmt.setString(2, "张三");
    				stmt.setInt(3, 20);
    				stmt.setString(4, "男");
    				//把参数添加到缓存区
    				stmt.addBatch();
    				//每20次发送一次参数
    				if(i%20==0){
    					//执行批处理命令
    					stmt.executeBatch();
    					//清空缓存区的参数
    					stmt.clearBatch();
    				}
    			}
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			JdbcUtil.close(stmt, conn);
    		}
    	}
    	
    }
    


  • 相关阅读:
    Django 框架篇(四) : 视图(view)详解 及 路由系统(url)
    Django 框架篇(三) : Django之模板
    Django 框架篇(二) : 创建APP之视图函数; model(模型)系统操作数据库之ORM操作;
    Django 框架篇: 一. Django介绍; 二. 安装; 三. 创建项目;
    212
    redux:applyMiddleware源码解读
    react 反模式——不使用jsx动态显示异步组件
    angular 动态组件类型
    webpack2-webpack.config.js配置
    tdd:(react + mocha)环境配置
  • 原文地址:https://www.cnblogs.com/mzywucai/p/11053477.html
Copyright © 2011-2022 走看看