从SQLSERVER binary 导入到ORCALE Blob字段

      
     //照片处理段
     InputStream photo = rs.getBinaryStream("ZA9998");

     //照片处理       
            if(photo!=null){
            BLOB blob = null;
            Ex1Con.setAutoCommit(false);

            PreparedStatement pstmt = Ex1Con.prepareStatement
           ("update t_gs_person set PERSON_PHOTO=empty_blob() where PERSON_ID=?"); 
            pstmt.setInt(1,perrow+1);
            pstmt.executeUpdate();
            pstmt.close();

            pstmt = Ex1Con.prepareStatement
           ("select PERSON_PHOTO from t_gs_person where PERSON_ID= ? for update");
            pstmt.setInt(1,perrow+1);
            ResultSet rset = pstmt.executeQuery();

            if (rset.next()) blob = (BLOB) rset.getBlob(1);
            try {

	           pstmt = Ex1Con.prepareStatement
                   ("update t_gs_person set PERSON_PHOTO=? where PERSON_ID=?");

		   OutputStream out = blob.getBinaryOutputStream();
		   int count = -1, total = 0;
		   byte[] data = new byte[(int)photo.available()];
		   photo.read(data);
		   out.write(data); 
		   photo.close();
		   out.close();

		   pstmt.setBlob(1,blob);
		   pstmt.setInt(2,perrow+1);
		   pstmt.executeUpdate();
		   pstmt.close(); 
		} catch (IOException e) {
		// TODO Auto-generated catch block
		   try {
			photo.close();
			} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
            }//照片处理if语句结束
     //照片处理结束


建议Statement对象名字区分开来,后来发现关闭对象时候,有状态对象未关闭。

你可能感兴趣的:(sqlserver)