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

最新文章

  1. P1774 最接近神的人_NOI导刊2010[树状数组 逆序对 离散化]
  2. PHPstorm的数据库功能
  3. 安装了VS2010 sp1 后再安装ASP.NET MVC 3.0的问题
  4. Odoo10 变化
  5. C2第五次作业解题报告
  6. 火狐 SSL 收到了一个弱临时 Diffie-Hellman 密钥的解决办法
  7. 可以这样去理解group by和聚合函数(转)
  8. 通过Eclipse创建SQLite数据库
  9. python调用Moxa PCOMM Lite通过串口Ymodem协议发送文件
  10. core文件找不到了
  11. 更新Xcode7 后 .dylib变成了.tbd的问题解决
  12. 一.初识java
  13. 学习C++模板,类模板
  14. vue使用vue-video-player在直播中的应用
  15. python+selenium十五:CSS与Jquery
  16. ALSA声卡驱动的DAPM(二)-建立过程
  17. spring ----> ResourceBundle [message] not found for MessageSource: Can't find bundle for base name message, local_zh
  18. ssh连接工具中文乱码问题
  19. poj1258 Agri-Net(Prime || Kruskal)
  20. SDUT -refresh的停车场(栈和队列)

热门文章

  1. centos上nginx的安装
  2. Week7:SVM难点记录
  3. HDU 2087 剪花布条 (简单KMP或者暴力)
  4. C#中参数传递
  5. Java Activiti 工作流引擎 springmvc SSM 流程审批 后台框架源码
  6. 苹果IPad客户端安装测试软件
  7. Ajax数据获取(未完待续。。。)
  8. python使用findall正则匹配出所有符合条件的字符串
  9. emlog 百度熊掌号提交插件-基于Emlog6.0.1特别版美化
  10. PHP目前比较常见的五大运行模式