1. 程式人生 > >操作大文字資料儲存在資料庫中 mysql中有個lob

操作大文字資料儲存在資料庫中 mysql中有個lob

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