JDBC学习笔记(4)——操作BLOB类型字段

1.MySQL BLOB类型

在这里插入图片描述

2.向数据表中插入和查找Blob

插入数据调用方法:setBlob()

	connection = JDBCUtils.getConnection();
	String sql = "insert into customers(name,email,birth,photo) values (?,?,?,?)";
	preparedStatement = connection.prepareStatement(sql);

	preparedStatement.setObject(2,"90798@gmail.com");
	preparedStatement.setObject(3,"1972-12-16");
	FileInputStream is = new FileInputStream(new File("111.png"));
	preparedStatement.setBlob(4,is);
	preparedStatement.execute();

查找数据调用getBlob():

Blob photo = resultSet.getBlob("photo");
//将Blob类型的字段下载下来以文件方式保存在本地
is = photo.getBinaryStream();
fos = new FileOutputStream("张宇.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read()) !=-1 ){
	fos.write(buffer,0,len);
}

3. 批量插入数据的操作

//使用批操作方法:addBatch()、executeBatch()、clearBatch()减少数据库的访问次数
			connection = JDBCUtils.getConnection();
			//设置不允许自动提交数据
            connection.setAutoCommit(false);
            String sql = "insert into goods(name)values(?)";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 1; i <= 20000; i++) {
                preparedStatement.setObject(1, i);
                //1.攒sql
                preparedStatement.addBatch();
                if (i%500 == 0){
                    //2.执行batch
                    preparedStatement.executeBatch();
                    //3.清空batch
                    preparedStatement.clearBatch();
                }
            }
            //提交数据
            connection.commit();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值