1、处理大文本
package com.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.utils.DButils;
//jdbc存大文本数据
public class Demo1 {
@Test
public void insert() throws SQLException, FileNotFoundException{
Connection con = null;
PreparedStatement st = null;
ResultSet result = null;
try {
con = DButils.getConnection();
String sql = "insert into testclob(id,resume) values(?,?)";
st = con.prepareStatement(sql);
st.setString(1,"1");
File file = new File("src/1.txt");
FileReader reader = new FileReader(file);
//设置大文本的列
st.setCharacterStream(2, reader, file.length());
int num = st.executeUpdate();
if(num>0){
System.out.println("插入成功");
}
}finally{
DButils.release(con, st, result);
}
}
//读取大文本数据
@Test
public void read() throws SQLException, IOException{
Connection con = null;
PreparedStatement st = null;
ResultSet result = null;
try {
con = DButils.getConnection();
String sql = "select id,resume from testclob where id='1'";
st = con.prepareStatement(sql);
result = st.executeQuery();
if(result.next()){
//String resume = result.getString("resume");不能用String保存,占用内存过大
Reader reader = result.getCharacterStream("resume");
FileWriter writer = new FileWriter("c:\\1.text");
try{
int len = 0;
char buffer[] = new char[1024];
while((len=reader.read(buffer))>0){
writer.write(buffer, 0, len);
}
}finally{
if(reader!=null){
reader.close();
}
writer.close();
}
}
}finally{
DButils.release(con, st, result);
}
}
}
2、处理二进制文件
package com.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.utils.DButils;
//jdbc存取二进制文件
public class Demo2 {
@Test
public void insert() throws SQLException, FileNotFoundException{
Connection con = null;
PreparedStatement st = null;
ResultSet result = null;
try{
con = DButils.getConnection();
String sql = "insert into testblob(id,image) values(?,?)";
st = con.prepareStatement(sql);
st.setString(1, "1");
File file = new File("src/1.jpg");
FileInputStream in = new FileInputStream(file);
st.setBinaryStream(2, in, file.length());
st.executeUpdate();
}finally{
DButils.release(con, st, result);
}
}
@Test
public void read() throws SQLException, IOException{
Connection con = null;
PreparedStatement st = null;
ResultSet result = null;
try{
con = DButils.getConnection();
String sql = "select id,image from testblob where id='1'";
st = con.prepareStatement(sql);
result = st.executeQuery();
if(result.next()){
51 InputStream in = result.getBinaryStream("image");
52 OutputStream out = new FileOutputStream("c:\\1.jpg");
53 try{
54 int len = 0;
55 byte[] buffer = new byte[1024];
56 while((len=in.read(buffer))>0){
57 out.write(buffer, 0, len);
58 }
59 }finally{
60 if(in!=null){
61 in.close();
62 }
63 if(out!=null){
64 out.close();
65 }
66 }
67 }
}finally{
DButils.release(con, st, result);
}
}
}