大数据对象这里主要指CLOB和BLOB两种类型的字段。
CLOB可以存储文本数据,而BLOB中可以存储二进制数据,如图片,电影等。而程序中处理这些大对象数据,必须调用PreparedStatement完成,所有的内容以IO流的方式从大文本字段中保存和读取。
PreparedStatement类中提供了两种方法写入数据
setAsciiStream():文本输入
setBinaryStream():二进制输入
另外,有几种读取数据方式:
getAsciiStream() 获取文本输出流
getBinaryStream() 获取二进制输出流
getClob() 获取CLOB数据
getBlob() 获取BLOB数据
1、处理CLOB数据
Clob表示大文本数据,在MySQL中有longtext表示大文本类型,最大保存4G数据。
首先在数据库bb中建表如下:
文本文档aa存放路径在”d:\aa.txt”,文本内容:
在eclipse中写入大文本数据
package fileOperation;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
public class CLOB_1 {
//将文本文件中的内容写入到数据库中
public static void main(String[] args) throws Exception{
Connection con=null;
PreparedStatement ps=null; //必须用预处理语句读取数据
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bb","root","123456");
ps=con.prepareStatement(" insert into userclob (name,note) values (?,?) ");
File f=new File("d:"+File.separator+"aa.txt"); //File.separator表示分隔符,更加通用
InputStream input=new FileInputStream(f);
ps.setString(1, "aa");
ps.setAsciiStream(2, input,(int) f.length()); //设置输入流
ps.executeUpdate();
ps.close();
con.close();
}
}
读取大文本数据
package fileOperation;
import java.io.InputStream;
import java.sql.*;
import java.util.Scanner;
public class CLOB_2 {
public static void main(String[] args) throws Exception{
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bb","root","123456");
ps=con.prepareStatement(" select name,note from userclob where id=? ");
ps.setInt(1,1);
rs=ps.executeQuery();
while (rs.next()) {
String name=rs.getString(1);
StringBuffer note=new StringBuffer();
InputStream input = rs.getAsciiStream(2);//接收全部大文本数据
Scanner scan=new Scanner(input);
scan.useDelimiter("\r\n"); //将文件换行作为分隔符
while(scan.hasNext()) {
note.append(scan.next()).append("\n"); //不断读取数据
}
System.out.println("内容为"+note);
System.out.println("*****************************************************************");
Clob c=rs.getClob(2);
String note1=c.getSubString(1, (int) c.length());
System.out.println("内容为"+note);
c.truncate(10); //读取10个长度的内容
System.out.println("读取部分内容: "+c.getSubString(1, (int)c.length()) );
//可以看到,两种方法均可以读取文件中的内容,而下面的这种方法更为简单
}
rs.close();
ps.close();
con.close();
}
}
结果如下:
内容为dsjfgnsk
vzkldfakjgbdn
azjsdjklzjsfhskdgkklbd
szvzgvsdbb
*****************************************************************
内容为dsjfgnsk
vzkldfakjgbdn
azjsdjklzjsfhskdgkklbd
szvzgvsdbb
读取部分内容: dsjfgnsk
可以看到,用CLOB读取数据更加简洁。
2、处理BLOB对象
BLOB操作与CLOB相似,专门用于存放二进制数据,如电影、图片等。在MySQL中定义了longblob类型变量,最高可以存放4G数据。
在数据库建表userblob,方法和上面一样。
将图片写入到数据表中
用到的图片存放到d盘:
在eclipse中编程:
package fileOperation;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class BLOB_1 {
public static void main(String[] args) throws Exception{
Connection con=null;
PreparedStatement ps=null; //必须用预处理语句读取数据
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bb","root","123456");
ps=con.prepareStatement(" insert into userblob (name,photo) values (?,?) ");
File f=new File("d:"+File.separator+"11.gif"); //File.separator表示分隔符
InputStream input=new FileInputStream(f);
ps.setString(1, "bb");
ps.setBinaryStream(2,input,(int)f.length());
ps.executeUpdate();
ps.close();
con.close();
}
}
新建一个图片,内容为空,保存在d盘,用于接收数据(也可以程序创建,如下一个例子)。读取内容,将图片信息保存。
package fileOperation;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
public class BLOB_2 {
public static void main(String[] args) throws Exception{
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bb","root","123456");
ps=con.prepareStatement(" select name,photo from userblob where id = ? ");
ps.setInt(1, 1);
rs=ps.executeQuery();
while(rs.next()) {
String name = rs.getString(1);
InputStream input =rs.getBinaryStream(2);
FileOutputStream output = new FileOutputStream(new File("d:"+File.separator+"12.gif"));
int temp=0;
while((temp=input.read())!=-1) {
output.write(temp);
}
input.close();
output.close();
}
rs.close();
ps.close();
con.close();
}
}
使用BLOB读取内容,更加简单:
package fileOperation;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;
public class BLOB_3 {
public static void main(String[] args) throws Exception{
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bb","root","123456");
ps=con.prepareStatement(" select name,photo from userblob where id = ? ");
ps.setInt(1, 1);
rs=ps.executeQuery();
while(rs.next()) {
String name = rs.getString(1);
Blob b=rs.getBlob(2);
File f=new File("d:"+File.separator+"13.gif");
if(f.exists()) {
f.delete();
}
else
{
f.createNewFile();
}
FileOutputStream output = new FileOutputStream(new File("d:"+File.separator+"13.gif"));
output.write(b.getBytes(1, (int) b.length()));
output.close();
}
rs.close();
ps.close();
con.close();
}
}