- 论坛徽章:
- 0
|
作者:dxadnwfn(gong c w)
版权:"厕所里冥想"
上传/更新时间:2007-07-30
功能:图片插入oracle
package com.nbcx.updataimg;
import java.sql.*;
import java.io.*;
import com.jbas.xsql.DbIdenUtil;
import oracle.sql.*;
/**
* @功能
* 在oracle下图片的上传
*
* @参数
* String table 图片插入到哪一张表;
* String info_joint 表的外键..就是对应表(具体的信息表)的主键
* String infile 文件路径与文件名
*
* @变量
* String Mxaid 图片表的主键
* String outerjoint 图片表的列名 如buyinfo_joint 就是buyinfo对应的图片表
* String String inssertsql 插入的SQL语句
* String selectsql 查询的SQL语句
* String updatesql 更新的SQL语句
* String del 删除的SQL语句
*
* @主要流程
* 1.拼成Sql 语句
*
* 2.得到驱动
*
* 3.删除原先有的图片
*
* 4.先插入主键与外键和一个空的Blob empty_blob() 空的Blob
* "insert into "+table+" (joint,"+outerjoint+",pic) values (?,?,empty_blob())"
*
* 5.在查询到要插入图片的那条记录
* "select pic from "+table+" where "+outerjoint+"= '"+info_joint+"' for update";
*
* 6.得到文件流.
*
* 7.插入图片到数据库
* "update "+table+" set pic = ? where "+outerjoint+" = ?";
*
* @注意
* "select pic from "+table+" where "+outerjoint+"= '"+info_joint+"' for update";
* 这里查询语句后where后的条件应加上单引号
*/
public class OracleUploadimg {
private PreparedStatement pstmt = null;
public void blobInsert(String table,String info_joint, String infile) {
// DbIdenUtil dbIdenUtil=new DbIdenUtil();
//String Maxid = dbIdenUtil.getIdentity(table);
String Maxid="5454";
String outerjoint="";
if("buyinfopic".equals(table)){
outerjoint="buyinfo_joint";
}else if("sellinfopic".equals(table)){
outerjoint="sellinfo_joint";
}else if("productpic".equals(table)){
outerjoint="product_joint";
}else if("orginfopic".equals(table)){
outerjoint="org_joint";
}
String inssertsql="insert into "+table+" (joint,"+outerjoint+",pic) values (?,?,empty_blob())";
String selectsql="select pic from "+table+" where "+outerjoint+"= '"+info_joint+"' for update";
String updatesql="update "+table+" set pic = ? where "+outerjoint+" = ?";
String del="DELETE FROM "+table+" where "+outerjoint+"="+info_joint;
/*
System.out.println("del = "+del);
System.out.println(inssertsql);
System.out.println(selectsql);
System.out.println(updatesql);
*/
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@60.190.22.3:1521:ORCL","EQAPP","EQLD");
conn.setAutoCommit(false);
//先删除原先图片
pstmt = conn.prepareStatement(del);
pstmt.executeUpdate();
//插入主键和外键
BLOB blob = null;
pstmt = conn.prepareStatement(inssertsql);
pstmt.setString(1,Maxid);
pstmt.setString(2,info_joint);
pstmt.executeUpdate();
// 查询外键,得到指针..其实这里查询主键也是一样的..都是唯一的
pstmt = conn.prepareStatement(selectsql);
// pstmt.setString(1,info_joint);
ResultSet rset = pstmt.executeQuery();
if (rset.next()) blob = (BLOB) rset.getBlob(1);
String fileName =infile;
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
System.out.println("file size = " + fin.available());
//把文件写入到内在\中
pstmt = conn.prepareStatement(updatesql);
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
/* byte[] data = new byte[(int)fin.available()];
fin.read(data);
out.write(data);*/
byte[] data = new byte[blob.getBufferSize()];
while ((count = fin.read(data)) != -1) {
total += count;
out.write(data, 0, count);
}
fin.close();
out.close();
//写入到数据库中
pstmt.setBlob(1,blob);
pstmt.setString(2,info_joint);
pstmt.executeUpdate();
pstmt.close();
conn.commit();
conn.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
blog日志,注意行版权哦,转载请注明
作者:dxadnwfn(gong c w)
版权:"厕所里冥想"
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/17667/showart_101418.html |
|