在实际开发中,程序需要把大文本或二进制数据保存到数据库。
大数据也称之为LOB(Large Objects),LOB又分为:
clob和blob
clob用于存储大文本。Text(mysql)
blob用于存储二进制数据,例如图像、声音、二进制文等。
对MySQL而言只有blob,而没有clob,mysql存储大文本采用的是Text,Text和blob分别又分为:
Test(可以存储多大的数据):TINYTEXT(256字节)、TEXT(64k)、MEDIUMTEXT(16M)和LONGTEXT(4G)
blob:TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB
下面是存取大文本的示例:
package cn.itcast.jdbc; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.Test; import cn.itcast.utils.JdbcUtils; /*创建数据库 create database day13; use day13; create table testclob ( id int primary key, resume text ); */ public class Demo1 { //大文本的存储 @Test public void save() throws Exception{ Connection conn =null; PreparedStatement st =null; ResultSet rs =null; try { conn =JdbcUtils.getConnection(); String sql ="insert into testclob (id,resume) values (?,?)"; st =conn.prepareStatement(sql); st.setInt(1,9); String path =Demo1.class.getClassLoader().getResource("abc.txt").getPath();//得到文件的路径 File file =new File(path); //创建文件对象 st.setCharacterStream(2,new FileReader(file), (int)file.length()); //创建一个流来传递大文本 st.executeUpdate(); }finally{ JdbcUtils.release(conn, st, rs); } } //取出大文本 @Test public void get() throws Exception{ Connection conn =null; PreparedStatement st =null; ResultSet rs =null; try { conn =JdbcUtils.getConnection(); String sql ="select id,resume from testclob where id =8"; st=conn.prepareStatement(sql); rs=st.executeQuery(); if(rs.next()){ //把列的数据作为流返回,然后放到缓冲中一点一点的返回,不至于内存溢出 Reader reader =rs.getCharacterStream("resume"); char buffer[] =new char [1024]; //new 一个char的缓冲 int len=0; FileWriter fw =new FileWriter("c:\\1.txt");//输出流 while ((len=reader.read(buffer))>0){ //把数据读到缓冲里去读取数据 fw.write(buffer,0,len); } fw.close(); reader.close(); } }finally{ JdbcUtils.release(conn, st, rs); } } }
以下是存取图片(二进制数据)示例:
package cn.itcast.jdbc; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.Test; import cn.itcast.utils.JdbcUtils; /* * create table testblob ( id int primary key, image blob ); alter table testblob modify image longblob; */ //二进制的存取 public class Demo2 { //存储图片 @Test public void save() throws Exception{ Connection conn =null; PreparedStatement st =null; ResultSet rs =null; try { conn =JdbcUtils.getConnection(); String sql ="insert into testblob(id,image) values(?,?)"; st =conn.prepareStatement(sql); st.setInt(1,4); String filePath=Demo2.class.getClassLoader().getSystemResource("1.jpg").getPath(); File file =new File (filePath); st.setBinaryStream(2,new FileInputStream(file),(int)file.length()); st.executeUpdate(); }finally{ JdbcUtils.release(conn, st, rs); } } //读取图片 @Test public void get() throws Exception{ Connection conn =null; PreparedStatement st =null; ResultSet rs =null; try { conn =JdbcUtils.getConnection(); String sql ="select id,image from testblob where id =4"; st=conn.prepareStatement(sql); rs=st.executeQuery(); if(rs.next()){ InputStream in = rs.getBinaryStream("image"); byte buffer []=new byte [1024]; //设置字节流缓冲 int len=0; FileOutputStream out =new FileOutputStream("c:\\1.jpg");//输出流 while ((len =in.read(buffer))>0){ //加载到缓冲中 out.write(buffer,0,len); } in.close(); out.close(); } }finally{ JdbcUtils.release(conn, st, rs); } } }