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();
}
}
}

}

posted @ 2016-08-15 16:03  G-&-D  阅读(3125)  评论(0编辑  收藏  举报