数据库blob语句
数据库blob语句
1.数据库结构
create table TEST
(
ID NUMBER,
AGE VARCHAR2(50),
TM VARCHAR2(50),
TEMPID VARCHAR2(20),
BLOBFILE BLOB
)
2.写数据
public void writeBlob(){
try {
String sql="insert into test(id,blobfile) values(?,?)";
Connection con=getConnction();
File f=new File("d:/a.txt");
InputStream in=new FileInputStream(f);
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1, 3);
ps.setBinaryStream(2, in,(int)f.length());
ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3. 读数据
public void readBlob(){
try {
String sql="select id,blobfile from test where id=?";
Connection con=getConnction();
//File f=new File("d:/a.txt");
// InputStream in=new FileInputStream(f);
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1, 3);
ResultSet res= ps.executeQuery();
while(res.next()){
ByteArrayOutputStream out=new ByteArrayOutputStream();
InputStream in=res.getBinaryStream("blobfile");
byte b[]=new byte[1024];
int len=0;
while((len=in.read(b))!=-1){
out.write(b, 0, len);
};
System.out.println(out.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}