BLOB:大数据,大对象,在数据库中用来存储超长文本的数据,例如图片等

将一张图片存储在mysql中,并读取出来(BLOB数据:插入BLOB类型的数据必须使用PreparedStatement,因为插入BLOB类型的数据无法使用字符串拼写):

------------------------------------------------------------------------------------------------------------------

package com.lanqiao.javatest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.crypto.Data;

import org.junit.Test;

import com.mysql.jdbc.ResultSetMetaData;

/*大数据,大对象,用来存储超长文本的数据,例如图片等;
* BLOB数据:插入BLOB类型的数据必须使用PreparedStatement,
* 因为插入BLOB类型的数据无法使用字符串拼写
* 调用方法:
* InputStream inputStream=new FileInputStream("Kn=sW70mL7A.jpg");
* preparedstatement.setBlob(4, inputStream);
* */
public class Test12 {
static Test1 t=new Test1();

//语句插入
@Test
public void testInsertBlob() throws Exception{
Connection connection=null;
PreparedStatement preparedstatement=null;
try {//往数据库张插入数据
connection=t.getConnection();
String sql="insert into customer (name,email,birth,picture) values(?,?,?,?)";
preparedstatement=connection.prepareStatement(sql);
preparedstatement.setString(1, "liquafd");
preparedstatement.setString(2, "fsdfdf");
preparedstatement.setDate(3, new Date(new java.util.Date().getTime()));

//向数据库中插入一张图片
//数据库中图片的类型是mediumblob,图片存储在程序的bin里面
InputStream inputStream=new FileInputStream("Kn=sW70mL7A.jpg");
preparedstatement.setBlob(4, inputStream);

//获取实时时间的方法Date date=new Date(new java.util.Date().getTame);

preparedstatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(preparedstatement!=null){
preparedstatement.close();
}
if(connection!=null){
connection.close();
}
}
}
//测试数据库是否连接成功
public void testBlob() throws Exception{
System.out.println(t.getConnection());
}
//查询插入的数据
public void getT() throws Exception{

Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;

try {
//获取connection连接
connection=t.getConnection();
//获取
String sql="select id,name,email,birth,picture from customer where id=36";
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();

if(resultSet.next()){
int id=resultSet.getInt(1);
String name=resultSet.getString(2);
String email=resultSet.getString(3);

Data birth=(Data) resultSet.getDate(4);
// preparedstatement.setDate(4, new Date(new java.util.Date().getTime())
//插入时使用

Blob picture=resultSet.getBlob(5);
InputStream in=picture.getBinaryStream();
OutputStream out=new FileOutputStream("Kn=sW70mL7A.jpg");

byte [] b=new byte[1024];
int len;
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
out.close();
in.close();
}



} catch (Exception e) {
e.printStackTrace();
}finally {
if (resultSet!=null) {
resultSet.close();
}
if (preparedStatement!=null) {
preparedStatement.close();
}
if (connection!=null) {
connection.close();
}
}
}

}

转载于:https://www.cnblogs.com/lxnlxn/p/5773305.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值