操作大文本数据存储在数据库中 mysql中有个lob
2015-08-09 18:02
573 查看
/*
create table t2(
id int primary key,
content longtext
);
*/
//插入大文本数据
@Test
public void testTextWrite() throws Exception{
Connection conn = JdbcUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("insert into t2 (id,content) values (?,?)");
stmt.setInt(1, 1);
File file = new File("src/jpm.txt");
Reader reader = new FileReader(file);
stmt.setCharacterStream(2, reader, (int)file.length());//MySQL驱动对于setCharacterStream(int,Reader,long)根本没有实现
stmt.executeUpdate();
JdbcUtil.release(null, stmt, conn);
}
//取出大文本数据
@Test
public void testTextRead() throws Exception{
Connection conn = JdbcUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("select * from t2 where id=1");
ResultSet rs = stmt.executeQuery();
if(rs.next()){
Reader r = rs.getCharacterStream("content");
//写到磁盘上
Writer w = new FileWriter("d:/jpm.txt");
char c[] = new char[1024];
int len = -1;
while((len=r.read(c))!=-1){
w.write(c, 0, len);
}
r.close();
w.close();
}
JdbcUtil.release(rs, stmt, conn);
}
/*
create table t3(
id int primary key,
content longblob
);
*/
//插入大二进制数据
@Test
public void testBlobWrite() throws Exception{
Connection conn = JdbcUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("insert into t3 (id,content) values (?,?)");
stmt.setInt(1, 1);
InputStream in = new FileInputStream("src/19.jpg");
stmt.setBinaryStream(2, in, in.available());
stmt.executeUpdate();
JdbcUtil.release(null, stmt, conn);
}
//取出大二进制数据
@Test
public void testBlobRead() throws Exception{
Connection conn = JdbcUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("select * from t3 where id=1");
ResultSet rs = stmt.executeQuery();
if(rs.next()){
InputStream in = rs.getBinaryStream("content");
//写到磁盘上
OutputStream out = new FileOutputStream("d:/a.jpg");
byte b[] = new byte[1024];
int len = -1;
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.close();
}
JdbcUtil.release(rs, stmt, conn);
}
create table t2(
id int primary key,
content longtext
);
*/
//插入大文本数据
@Test
public void testTextWrite() throws Exception{
Connection conn = JdbcUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("insert into t2 (id,content) values (?,?)");
stmt.setInt(1, 1);
File file = new File("src/jpm.txt");
Reader reader = new FileReader(file);
stmt.setCharacterStream(2, reader, (int)file.length());//MySQL驱动对于setCharacterStream(int,Reader,long)根本没有实现
stmt.executeUpdate();
JdbcUtil.release(null, stmt, conn);
}
//取出大文本数据
@Test
public void testTextRead() throws Exception{
Connection conn = JdbcUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("select * from t2 where id=1");
ResultSet rs = stmt.executeQuery();
if(rs.next()){
Reader r = rs.getCharacterStream("content");
//写到磁盘上
Writer w = new FileWriter("d:/jpm.txt");
char c[] = new char[1024];
int len = -1;
while((len=r.read(c))!=-1){
w.write(c, 0, len);
}
r.close();
w.close();
}
JdbcUtil.release(rs, stmt, conn);
}
/*
create table t3(
id int primary key,
content longblob
);
*/
//插入大二进制数据
@Test
public void testBlobWrite() throws Exception{
Connection conn = JdbcUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("insert into t3 (id,content) values (?,?)");
stmt.setInt(1, 1);
InputStream in = new FileInputStream("src/19.jpg");
stmt.setBinaryStream(2, in, in.available());
stmt.executeUpdate();
JdbcUtil.release(null, stmt, conn);
}
//取出大二进制数据
@Test
public void testBlobRead() throws Exception{
Connection conn = JdbcUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement("select * from t3 where id=1");
ResultSet rs = stmt.executeQuery();
if(rs.next()){
InputStream in = rs.getBinaryStream("content");
//写到磁盘上
OutputStream out = new FileOutputStream("d:/a.jpg");
byte b[] = new byte[1024];
int len = -1;
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.close();
}
JdbcUtil.release(rs, stmt, conn);
}
相关文章推荐
- liunx 系统下mysql数据库的jdbc连接使用
- Mysql主从搭建脚本
- mysql的日期查询操作,查询最近一天,一周,一个月
- mysql sql查询过程分析之explain关键字
- 装好MYsql后显示Access denied for user 'root'@'localhost' (using password: NO)
- 项目开发中用到的一些MySQL语句
- [转]mysql组合索引与字段顺序
- MySQL性能调优的14板斧
- win7中mysql5.7.7的配置部署
- MySQL的数据类型
- 解决MySQL查询不区分大小写
- ERROR! MySQL is running but PID file could not be found
- mysql 锁机制
- 压缩包Mysql的安装以及卸载的简单方法
- Mysql那些琐碎
- Mysql常用函数
- windows下mysql忘记root密码的解决方法
- 源码安装mysql
- Mysql之存储过程回顾
- mysql之触发器trigger