1. 往mysql中读写字符文本

public class Demo1 {

	/* 创建数据库
create database LOBTest;
use LOBTest;
create table testclob
(
id int primary key auto_increment,
resume text
); */ @Test
public void add() {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try
{
conn = JdbcUtils.getConnection();
String sql = "insert into testclob(resume) values(?)";
st = conn.prepareStatement(sql);
String path = Demo1.class.getClassLoader().getResource("db.properties").getPath();
File file = new File(path);
FileReader fr = new FileReader(file);
st.setCharacterStream(1, fr, file.length());
int num = st.executeUpdate();
if(num > 0)
System.out.println("插入成功!!");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
JdbcUtils.release(conn, st, rs);
}
} @Test
public void read()
{
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try
{
conn = JdbcUtils.getConnection();
String sql = "select resume from testclob where id = ?";
st = conn.prepareStatement(sql);
st.setInt(1, 2);
rs = st.executeQuery();
if(rs.next())
{
Reader reader = rs.getCharacterStream("resume");
FileWriter fw = new FileWriter("write.txt");
char[] buffer = new char[1024];
int len = 0;
while( (len=reader.read(buffer)) > 0)
{
fw.write(buffer, 0, len);
}
fw.close();
reader.close();
} }
catch (Exception e)
{
e.printStackTrace();
}
finally
{
JdbcUtils.release(conn, st, rs);
}
} }

2. 往mysql中读写二进制文件

public class Demo2 {

	/*
create table testblob
(
id int primary key auto_increment,
image blob
);
*/ @Test
public void add()
{
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try
{
conn = JdbcUtils.getConnection();
String sql = "insert into testblob(image) values(?)";
st = conn.prepareStatement(sql); String path = Demo1.class.getClassLoader().getResource("1.JPG").getPath();
File file = new File(path);
FileInputStream sf = new FileInputStream(file);
st.setBinaryStream(1, sf, file.length());
int num = st.executeUpdate();
if(num > 0)
System.out.println("插入成功!!");
sf.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
JdbcUtils.release(conn, st, rs);
}
} @Test
public void read()
{
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try
{
conn = JdbcUtils.getConnection();
String sql = "select image from testblob where id = ?";
st = conn.prepareStatement(sql);
st.setInt(1, 1);
rs = st.executeQuery();
if(rs.next())
{
InputStream is = rs.getBinaryStream("image");
byte[] buffer = new byte[1024];
int len = 0;
FileOutputStream fos = new FileOutputStream("1_back.JPG");
while( (len=is.read(buffer))>0 )
{
fos.write(buffer, 0, len);
}
fos.close();
is.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
JdbcUtils.release(conn, st, rs);
}
} }

最新文章

  1. Struts2框架简介和示例
  2. Modernizr.js:为HTML5和CSS3而生!
  3. 安卓界面控件屏幕居中Layout例子
  4. 【转】PHP调试开发工具你认识多少?
  5. 面向服务体系架构(SOA)和数据仓库(DW)的思考基于 IBM 产品体系搭建基于 SOA 和 DW 的企业基础架构平台
  6. Hadoop入门程序WordCount的执行过程
  7. python中时间日期格式化符号
  8. AI 行为树
  9. Python Socket,How to Create Socket Cilent? - 网络编程实例
  10. Telerik_2012_Q3 破解全套下载链接
  11. C语言随笔_printf输出多行
  12. 通信协议:HTTP、TCP、UDP(转)
  13. mongodb系列之--分片的原理与配置
  14. ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
  15. F4 help for month
  16. 跨域cors中如何传递cookie(前端为什么无法向后端传递cookie?)
  17. sql server 视图的操作
  18. Java-Runoob-高级教程-实例-数组:04. Java 实例 – 数组反转
  19. IIs8 svc
  20. MR案例:内连接代码实现

热门文章

  1. 《从零开始学Swift》学习笔记(Day 29)——访问级别
  2. 一篇搞定SQLAlchemy--关系对象映射
  3. 【题解】CF611H New Year and Forgotten Tree
  4. Python3.6全栈开发实例[017]
  5. zookeeper单机伪集群配置
  6. 学习Hive和Impala必看经典解析
  7. 20170411 F110初始界面-建议清单
  8. jvm性能监控工具
  9. 剑指offer 面试24题
  10. 分享几个基于 Yii2 的开源项目