圆外哈灵面馆 大众点评怎么样,好不好的默认点评

使用Java向Oracle数据库的BLOB数据列中插入数据 - 生活学习 - ITeye技术网站
博客分类:
源地址:
向Oracle数据库的大对象lob字段中插入数据的步骤
public int insertFileInfo(Eg_attachsfiles attachsfile,File file) {
* 向Orcale数据库的BLOB数据列插入数据的操作分一下几步
* 在操作这几步之前需要注意的是:关闭SQL语句的自动提交,也就是conn.setAutoCommit(false);
* 等操作完成后手动mit();
int result = 0;
if(attachsfile!=null){
* 第一步:
* 创建SQL语句,需要注意的是BLOB数据类型的那一列应先插入空BLOB数据,用于插入的函数是
* empty_BLOB();
String sql = "insert into eg_attachsfiles(FILE_NAME,FILE_SIZE,FILE_TYPE,CONTENT,FILE_SAVE,APPLYMATTERSNO) values('"+attachsfile.getFileName()+"',"+attachsfile.getFileSize()+",'"+attachsfile.getFileType()+"',EMPTY_BLOB(),'"+attachsfile.getFileSave()+"',"+attachsfile.getApplymattersNo()+")";
if(attachsfile.getMemo()!=null && !"".equals(attachsfile.getMemo())){
sql = "insert into eg_attachsfiles(FILE_NAME,FILE_SIZE,FILE_TYPE,CONTENT,FILE_SAVE,MEMO,APPLYMATTERSNO) values('"+attachsfile.getFileName()+"',"+attachsfile.getFileSize()+",'"+attachsfile.getFileType()+"',EMPTY_BLOB(),'"+attachsfile.getFileSave()+"','"+attachsfile.getMemo()+"',"+attachsfile.getApplymattersNo()+")";
* 第二步:
* 创建查询语句,查询刚才插入的那条数据的BLOB列表在查询语句的结尾必须添加for update
* 从而锁定要查询的列
String searchSql = "Select CONTENT From eg_attachsfiles where ID=(Select max(ID) From eg_attachsfiles) for update";
OutputStream outStream =
InputStream fin =
//执行新增
result = this.executeUpdate(sql, null);
//执行查询
this.res = this.executeQuery(searchSql, null);
if(res.next()){
//使用getBlob(1)java中的Blob对象
java.sql.Blob
b = res.getBlob(1);
//将java中的Blob对象转换重Oracle中的BLOB对象
oracle.sql.BLOB blob = (oracle.sql.BLOB)
//使用BLOB对象的getBinaryOutputStream()创建输出流对象
outStream = blob.getBinaryOutputStream();
//使用传进来的Flie对象创建输入流对象
fin = new FileInputStream(file);
//创建缓存
byte[] buff = new byte[(int)file.length()];
int index = 0;
//循环读取数据并把数据写入输出流中
while((index=fin.read(buff))!=-1){
outStream.write(buff);
} catch (Exception e) {
("把上传的文件写数据库当中时异常,异常发生在com.future.pubnet.dao.impl.Eg_attachsfilesDaoImpl类的insertFileInfo(Eg_attachsfiles attachsfile)方法,异常信息"+e.getMessage(),e.fillInStackTrace());
new PubnetException(e.getMessage(),e);
//关闭流对象
outStream.close();
fin.close();
} catch (IOException e) {
("关闭流对象异常,异常发生在com.future.pubnet.dao.impl.Eg_attachsfilesDaoImpl类的insertFileInfo(Eg_attachsfiles attachsfile)方法,异常信息"+e.getMessage(),e.fillInStackTrace());
new PubnetException(e.getMessage(),e);
this.closeRes();
this.closePstmt();
注意:如果你使用JNDI连接数据库时就需要把发布到服务器中的classes12.jar删掉,因为
使用JNDI连接池时服务器本身已经复制进去一个classes12.jar,如果不删除两个jar文件会有冲突,项目报类转换异常(ClassCaseException)。
浏览: 53040 次
来自: 西安
非常好,很谢谢
头像挺个性的 呵呵、、、
谢谢 楼主 正需要呢 呵呵
[u][/u]引用[b][/b]列表: [list] [*]文 ...
不过可惜没写几个好的实例。

我要回帖

更多关于 行圆汽车好不好 的文章

 

随机推荐