往oracle里面类型为blob写入时,必须先插入一个empty_blob,实行update……
具体java里面写入blob的代码如下:
public class applyPhotoBLOB {
final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection connSDC = null;
Connection conn = null;
String sConnStr = "jdbc:oracle:thin:@127 0.0 1 1521:sledu";
String sConnStrSDC = "jdbc:oracle:thin:@10 10 8.12:1521:rac2";
String sDBUid = "test";
String sDBPwd = "test";
String sDBUidSDC = "sdcmanager";
String sDBPwdSdc = "sdcmanager_888";
try
{
applyPhotoBLOB apply = new applyPhotoBLOB();
connSDC = apply.getConn(sConnStrSDC,sDBUidSDC,sDBPwdSdc);
if(connSDC!=null)
{
apply.testBOLB(connSDC);
}
System.out.println("处理完成!");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
if(conn!=null) conn.close();
if(connSDC!=null) connSDC.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
public void testBOLB(Connection conn) throws Exception
{
String strSQL = "Insert Into BKS_XSZPXX(XH,ZPLXM,ZP) Values('3071801040','1',empty_blob())";
updateTable1(strSQL,conn);
conn.setAutoCommit(false);
strSQL = "Select ZP from BKS_XSZPXX where XH='3071801040' For Update";
Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement();
rs = stmt.executeQuery(strSQL);
rs.next();
BLOB blob = (BLOB) rs.getBlob("ZP");
OutputStream os = blob.getBinaryOutputStream();// 建立输出流
BufferedOutputStream output = new BufferedOutputStream(os);
BufferedInputStream input = new BufferedInputStream(new File("F:/3071801040.jpg").toURL().openStream());
byte[] buff = new byte[2048000]; //用做文件写入的缓冲
int bytesRead;
while(-1 != (bytesRead = input.read(buff, 0, buff.length)))
{
output.write(buff, 0, bytesRead);
//System.out.println(bytesRead);
}
output.close();
input.close();
rs.close();
conn.commit();
conn.setAutoCommit(true);
stmt.close();
}
private int updateTable1(String strSQL,Connection conn) throws Exception
{
PreparedStatement stmt = null;
int result = 0;
try
{
stmt = conn.prepareStatement(strSQL);
result = stmt.executeUpdate();
}
catch(Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
stmt.close();
}
return result ;
}
public Connection getConn(String StrConn,String uid,String pwd) throws Exception
{
Connection conn = null;
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(StrConn,uid,pwd);
}
catch (Exception e)
{
throw new Exception(e.getMessage());
}
return conn;
}
}
另外:放入business里面的时候,其zp最好定义为InputStream
转载,仅供参考。