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

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM