BLOB:大數據,大對象,在數據庫中用來存儲超長文本的數據,例如圖片等

將一張圖片存儲在mysql中,並讀取出來(BLOB數據:插入BLOB類型的數據必須使用PreparedStatement,由於插入BLOB類型的數據沒法使用字符串拼寫):java

------------------------------------------------------------------------------------------------------------------mysql

package com.lanqiao.javatest;sql

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;xml

/*大數據,大對象,用來存儲超長文本的數據,例如圖片等;
* 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();
}
}
}

}對象

相關文章
相關標籤/搜索