JavaEE之JDBC进阶使用

JDBC

JDBC批处理

1、为什么需要批处理?

之前:一次操作只能发送一条sql语句到数据库服务器,效率并不高!如果要插入2000条记录,那么必须发送2000条sql语句。

如果IO流的话,一次写出一个字节,显然效率效率并不高,所以可以使用缓存字节数组提高每次写出的效率。

现在:插入2000条记录,但现在使用sql缓存区,一次发送多条sql到数据库服务器执行。这种做法就叫做批处理。

2、JDBC批处理的API

        statement 批处理:

                  void  addBatch(String sql)   将sql语句添加到缓冲区

                  int[]   executeBatch()       执行批处理命令  发送所有的sql语句

                  void    clearBatch()  清空缓存区

        PreparedStatement批处理:               

                 void       addBatch() 添加参数到缓存区

                 int[]        executeBatch() 执行批处理命令。 发送所有缓存区的sql

                 void        clearBatch()  清空sql缓存区

现进行测试:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
/**
 * 同时插入2000条数据
 * @author APPle
 * 结论:
 * 1) mysql数据库不支持PreparedStatement优化,而且不支持批处理优化
 * 2) oracle数据库即支持PreparedStatement优化,也支持批处理优化   
 *
 */
public class Demo1 {
	
	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);
		}
	}
	
}

测试结论: 

        1) mysql数据库不支持PreparedStatement优化,而且不支持批处理优化

        2) oracle数据库即支持PreparedStatement优化,也支持批处理优化   
3、JDBC读取大文件数据

                        mysql:

                                   字符串:varchar  char     65535

                                   大文本数据: tinytext , longtext   ,text

                                   字节:  bit 

                                   大字节文件:tinyblob(255byte),blob(64kb),MEDIUMBLOB(约16M) longblob(4GB)

                          oracle:

                                   字符串: varchar2  char     65535

                                   大文本数据: clob

                                   字节: bit

                                   大字节文件: blob
3.1 JDBC操作字符文件
/**
 * 对大文本数据处理
 * @author APPle
 *
 */
public class Demo1 {

	/**
	 * 文件保存到数据中
	 */
	@Test
	public void testWrite(){
		Connection conn = null;
		PreparedStatement stmt = null;
		try{
			//获取连接
			conn = JdbcUtil.getConnection();
			//创建PreparedStatement
			String sql = "INSERT INTO test1(content) VALUES(?)";
			stmt =conn.prepareStatement(sql);
			//设置参数
			/**
			 * 参数一: 参数位置
			 * 参数二: 输入字符流
			 */
			/**
			 * 读取本地文件,返回输入字符流
			 */
			FileReader reader = new FileReader(new File("e:/Demo1.java"));
			stmt.setClob(1, reader);
			//执行sql
			int count = stmt.executeUpdate();
			System.out.println("影响了"+count+"行");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.close(conn, stmt, null);
		}
	}
	
	/**
	 * 从数据中读取文本内容
	 */
	@Test
	public void testRead(){
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			//获取连接
			conn = JdbcUtil.getConnection();
			String sql = "SELECT * FROM test1 where id=?";
			stmt = conn.prepareStatement(sql);
			//设置参数
			stmt.setInt(1, 2);
			//执行sql,返回结果集
			rs = stmt.executeQuery();
			if(rs.next()){
				//方式一:当做字符串取出数据
				/*
				String content = rs.getString("content");
				System.out.println(content);
				*/
				
				//方式二:返回输入流形式
				Clob clob = rs.getClob("content");
				Reader reader = clob.getCharacterStream();
				//写出到文件中
				FileWriter writer = new FileWriter(new File("e:/Demo2.java"));
				char[] buf = new char[1024];
				int len = 0;
				while( (len=reader.read(buf))!=-1){
					writer.write(buf, 0, len);
				}
				//关闭流
				writer.close();
				reader.close();
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.close(conn, stmt, rs);
		}
	}
}
3.2 JDBC操作字节文件
/**
 * 对字节文件处理
 * @author APPle
 *
 */
public class Demo2 {

	/**
	 * 文件保存到数据库中
	 */
	@Test
	public void testWrite(){
		Connection conn = null;
		PreparedStatement stmt = null;
		try{
			//获取连接
			conn = JdbcUtil.getConnection();
			String sql = "insert into test2(content) values(?)";
			stmt = conn.prepareStatement(sql);
			//设置参数
			/**
			 * 参数一:参数位置
			 * 参数二:输入字节流
			 */
			/**
			 * 读取本地文件
			 */
			InputStream in = new FileInputStream(new File("e:/abc.wmv"));
			//stmt.setBlob(1, in);
			stmt.setBinaryStream(1, in);
			//执行
			stmt.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.close(conn, stmt, null);
		}
	}
	
	/**
	 * 注意: mysql数据库默认情况下,只能存储不超过1m的文件,由于max_allowed_packet变量的限制
	 *   可以修改: %mysql%/my.ini文件, 修改或添加max_allowed_packet变量,然后重启mysql即可!!
	 */
	

	/**
	 * 从数据中读取字节内容
	 */
	@Test
	public void testRead(){
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			//获取连接
			conn = JdbcUtil.getConnection();
			String sql = "SELECT * FROM test2 where id=?";
			//获取PreparedStatement
			stmt = conn.prepareStatement(sql);
			//设置参数
			stmt.setInt(1, 1);
			//执行sql
			rs = stmt.executeQuery();
			if(rs.next()){
				//返回输入流
				//InputStream in = rs.getBinaryStream("content");
				InputStream in = rs.getBlob("content").getBinaryStream();
				//写出文件中
				FileOutputStream out = new FileOutputStream(new File("e://3.jpg"));
				byte[] buf = new byte[1024];
				int len = 0;
				while((len=in.read(buf))!=-1){
					out.write(buf, 0, len);
				}
				//关闭流
				out.close();
				in.close();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			JdbcUtil.close(conn, stmt, rs);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值