1. 大文本的存取
package com.cn.lob;
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 com.cn.Util.JdbcUtil;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2017年8月3日18:43:27
* Desc:大文本的存取
use mydb
create table lob(
id int primary key,
content longtext
);
*/
public class ClobDemo {
@Test
public void testAdd() throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = JdbcUtil.getConnection();
String sql = "insert into lob(id, content) values(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 3);
//大数据要使用流的形式
File file = new File("d:/a.txt");
FileReader reader = new FileReader(file);
pstmt.setCharacterStream(2, reader, file.length());
int flag = pstmt.executeUpdate();
if(flag > 0){
System.out.println("插入成功");
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.close(conn, pstmt, null);
}
}
@Test
public void testRead() throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "select * from lob where id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 2);
//大数据要使用流的形式
File file = new File("d:/b.txt");
rs = pstmt.executeQuery();
if(rs.next()){
Reader reader = rs.getCharacterStream("content");
FileWriter writer = new FileWriter(file);
char[] buff= new char[1024];
int len = -1;
while((len = reader.read(buff)) != -1){
writer.write(buff, 0, len);
}
writer.close();
reader.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.close(conn, pstmt, rs);
}
}
}
2. 大二进制数据的存取
package com.cn.lob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.junit.Test;
import com.cn.Util.JdbcUtil;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2017年8月3日19:27:19
* Desc:大二进制数据的存取
use mydb
create table lob2(
id int primary key,
content longblob
);
*/
public class BlobDemo {
@Test
public void testAdd() throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
try{
conn = JdbcUtil.getConnection();
String sql = "insert into lob2(id, content) values(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 3);
//大数据要使用流的形式
InputStream in = new FileInputStream("d:/me.jpg");
pstmt.setBinaryStream(2, in, in.available());
int flag = pstmt.executeUpdate();
if(flag > 0){
System.out.println("插入成功");
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.close(conn, pstmt, null);
}
}
@Test
public void testRead() throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "select * from lob2 where id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 3);
//大数据要使用流的形式
File file = new File("d:/b.txt");
rs = pstmt.executeQuery();
if(rs.next()){
InputStream in = rs.getBinaryStream("content");
OutputStream out = new FileOutputStream("d:/me2.jpg");
byte[] buff= new byte[1024];
int len = -1;
while((len = in.read(buff)) != -1){
out.write(buff, 0, len);
}
out.close();
in.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JdbcUtil.close(conn, pstmt, rs);
}
}
}