关于把数据保存到oracle中的blob字段

 首先用这么一个form字段的属性去保存这个blob字段的值。
 <input style="width:100%" type="file" name="UploadFile" > </td>   
 然后就是调用savecmd这个动作,需要注意的是,在这个时候form表单的属性区别已经是
 <form name="formDetail" method="post" encType="multipart/form-data">
 这就提示在逻辑中数据的提取需要用到不一样的request的方法
 
 进入逻辑后需要用到一个关键那就是SaveFile()这个对象
 重点就是看:Vector vec = sf.getFileInfo(super.getSmartUpload());
 进入逻辑后,
 public Vector getFileInfo(SmartUpload su){
System.out.println("+++++++++++su::"+su); 
System.out.println("+++++++++++su.getFiles.getCount()::"+su.getFiles().getCount());
  Vector vec = new Vector();

  for (int i=0;i<su.getFiles().getCount();i++) {
   
   Hashtable hble = new Hashtable();
    
    com.jspsmart.upload.File file = su.getFiles().getFile(i);

   if (file.isMissing()) continue; 
   
   Hashtable htable = new Hashtable();
   htable.put("filename",file.getFileName());
   htable.put("file",file);
   htable.put("FieldName",file.getFieldName());
   htable.put("fileExt", file.getFileExt());
   vec.add(htable);
    
  }
  return vec;
 }
}
 用这段代码实现的是从添加的目录中找到需要上传的文件名字,以及是blob字段的名字。
 代码中的file对象是比较重要的一个对象
 首先到的是SmartUpload的这个对象里面
 直接进入到SmartUpload这个类里面
 public class SmartUpload
{

    public SmartUpload()
    {
        m_totalBytes = 0;
        m_currentIndex = 0;
        m_startData = 0;
        m_endData = 0;
        m_boundary = new String();
        m_totalMaxFileSize = 0L;
        m_maxFileSize = 0L;
        m_deniedFilesList = new Vector();
        m_allowedFilesList = new Vector();
        m_denyPhysicalPath = false;
        m_forcePhysicalPath = false;
        m_contentDisposition = new String();
        m_files = new Files();
        m_formRequest = new Request();
    }
 其实里面用的也就是一个getFiles这个方法,返回的是m_files这个属性,继续追踪下去,你就会看到
 实际上跑到的是Files这个文件
 public class Files
{

    Files()
    {
        m_files = new Hashtable();
        m_counter = 0;
    }

    protected void addFile(File newFile)
    {
        if(newFile == null)
        {
            throw new IllegalArgumentException("newFile cannot be null.");
        } else
        {
            m_files.put(new Integer(m_counter), newFile);
            m_counter++;
            return;
        }
    }

    public File getFile(int index)
    {
        if(index < 0)
            throw new IllegalArgumentException("File's index cannot be a negative value (1210).");
        File retval = (File)m_files.get(new Integer(index));
        if(retval == null)
            throw new IllegalArgumentException("Files' name is invalid or does not exist (1205).");
        else
            return retval;
    }

 最终返回的是一个file对象变量
 通过return retval去实现这个效果。
 
 也不知道是通过什么实现的,返回的这个file就具有了上传文件的类型,名称,以及路径,还有它的数据库对应的字段名称
 
 在执行玩getFileInfo()这个方
 法后,就可以得到一个包含要上传到blob字段里面文件的各个方面的属性。
 
 然后继续执行下一步的操作。
 然后通过vec.size()就可以得到一个hashtalbe然后就可以把从上面得到的有效东西放在起初从prefix里得到的有效值。
 其中包含:文件名,以及blob字段的名字.
 

 在存放blob字段数据的时候需要执行不一样的方法:
 
 下面来比较这两个方法:
 

 protected boolean executeUpdate(
  String tableName,
  Hashtable data,
  String condition) {
  Connection conn = DataSourceFactory.create().getConnection();
  if (conn == null) {
   setMessage("对不起,获取数据库连接时出现错误!");
   return false;
  }
  Hashtable newData = toUperCaseHashtable(data);
  StringBuffer metaSql = new StringBuffer("select "); //用于获取表结构的Sql语句
  StringBuffer sql = new StringBuffer("update ");
  sql.append(tableName);
  sql.append(" set ");
  Enumeration items = newData.keys();
  String item;
  while (items.hasMoreElements()) {
   item = items.nextElement().toString();
   sql.append(item);
   sql.append("=?,");
   metaSql.append(item);
   metaSql.append(",");
  }
  if (sql.charAt(sql.length() - 1) == ',')
   sql.deleteCharAt(sql.length() - 1);
  if (metaSql.charAt(metaSql.length() - 1) == ',')
   metaSql.deleteCharAt(metaSql.length() - 1);
  sql.append(" where ");
  sql.append(condition);
  metaSql.append(" from ");
  metaSql.append(tableName);
  metaSql.append(" where rownum <= 1");

  PreparedStatement pre = null;
  PreparedStatement preMeta = null; //用于获取表结构的PreparedStatement对象
  ResultSetMetaData rsmd = null; //用于获取表结构的ResultSetMetaData对象
  ResultSet rs = null; //用于获取表结构的ResultSet对象
  
  try {
   conn.setAutoCommit(false);
   preMeta = conn.prepareStatement(metaSql.toString());
   rs = preMeta.executeQuery();
   rsmd = rs.getMetaData();
   pre = conn.prepareStatement(sql.toString());
   Object columnValue;
   for (int i = 1; i <= rsmd.getColumnCount(); i++) {
    columnValue = newData.get(rsmd.getColumnName(i).toUpperCase());
    
    pre =
     setColumnValue(
      conn,
      pre,
      i,
      rsmd.getColumnType(i),
      columnValue);
   }
   pre.execute();

   conn.commit();
   effRow = pre.getUpdateCount();
   return true;
  } catch (ParseException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("数据转换错误:" + e.getMessage());
   setMessage("数据转换错误:" + e.getMessage());
   return false;
  } catch (SQLException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("插入记录错误:" + e.getMessage());
   System.out.println("错误的SQL语句为: " + sql.toString());
   System.out.print("错误时的插入数据为:" + newData);
   setMessage("插入记录错误:" + e.getMessage());
   return false;
  } catch (IOException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("文件数据读取错误:" + e.getMessage());
   setMessage("文件数据读取错误:" + e.getMessage());
   return false;
  } catch (Exception e) {
   try {
    conn.rollback();
   } catch (Exception e1) {
    e1.printStackTrace();
   }
   System.out.println("未知错误:" + e.getMessage());
   System.out.println("错误时的SQL语句为: " + sql.toString());
   System.out.print("错误时的插入数据为:" + newData);
   setMessage("对不起,发生未知错误!\\n" + e.getMessage());
   return false;
  } finally {
   try {
    if (preMeta != null) {
     preMeta.close();
    }
   } catch (SQLException e) {
    System.out.println("关闭prepareStatement对象时错误:" + e.getMessage());
   }
   try {
    if (pre != null) {
     pre.close();
    }
   } catch (SQLException e) {
    System.out.println("关闭prepareStatement对象时错误:" + e.getMessage());
   }
   try {
    if (conn != null) {
     conn.setAutoCommit(true);
    }
   } catch (SQLException e) {
    System.out.println("恢复Connection对象时错误:" + e.getMessage());
   }
   try {
    if (conn != null) {
     conn.close();
    }
   } catch (SQLException e) {
    System.out.println("关闭Connection对象时错误:" + e.getMessage());
   }
  }
 }

 下面这个是用来主攻存放blob字段数据的:
 

 protected boolean updateLob(
  String table,
  Hashtable data,
  String conditions) {
  if (table == null) {
   System.out.println("没有指定更新表!");
   return false;
  }
  if (data == null) {
   System.out.println("没有指定要更新的数据!");
   return false;
  }
  if (conditions == null) {
   System.out.println("没有用于定位记录的条件!");
   return false;
  }
  Hashtable newData = toUperCaseHashtable(data);
  Connection conn = DataSourceFactory.create().getConnection();
  if (conn == null) {
   setMessage("对不起,获取数据库连接失败!");
   return false;
  }
  StringBuffer sql = new StringBuffer("select ");
  Enumeration items = newData.keys();
  String item;
  while (items.hasMoreElements()) {
   item = items.nextElement().toString();
   sql.append(item);
   sql.append(",");
  }
  if (sql.charAt(sql.length() - 1) == ',')
   sql.deleteCharAt(sql.length() - 1);
  sql.append(" from ");
  sql.append(table);
  sql.append(" where ");
  sql.append(conditions);
  sql.append(" for update");
  PreparedStatement pre = null;
  ResultSet rs = null;
  ResultSetMetaData rsmd = null;
  try {
   conn.setAutoCommit(false);
   pre = conn.prepareStatement(sql.toString());
   rs = pre.executeQuery();
   if (rs.next()) {
    rsmd = rs.getMetaData();
    BLOB blob = null;
    CLOB clob = null;
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
     switch (rsmd.getColumnType(i)) {
      case Types.BLOB :
       blob = (BLOB) rs.getBlob(i);
       if (blob != null) {
        blob.trim(0);
        OutputStream blobout =
         blob.getBinaryOutputStream();
        Object obj = newData.get(rsmd.getColumnName(i));
        if (obj instanceof File) {
         blobout.write(((File) obj).getBytes());
        } else if (obj instanceof String) {
         blobout.write(
          (new BASE64Decoder()).decodeBuffer(
           obj.toString()));
        }
        blobout.flush();
        blobout.close();
       }
       break;
      case Types.CLOB :
       clob = (CLOB) rs.getClob(i);
       if (clob != null) {
        //System.out.println("CLOB*****");
        clob.trim(0);
        Writer clobWriter =
         clob.getCharacterOutputStream();
        if (newData
         .get(rsmd.getColumnName(i))
         .toString()
         .equals("")) {
         clobWriter.write(" ");
        } else {
         clobWriter.write(
          newData
           .get(rsmd.getColumnName(i))
           .toString());
        }
        clobWriter.flush();
        clobWriter.close();
       }
       break;
      default :
       break;
     }
    }
    conn.commit();
    return true;
   }
   System.out.println("没有检索到要更新的记录!");
   System.out.println("错误的SQL语句为: " + sql.toString());
   return false;
  } catch (SQLException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("修改LOB数据时错误:" + e.getMessage());
   System.out.println("错误的SQL语句为: " + sql.toString());
   System.out.println("错误的数据为: " + data);
   setMessage("对不起,插入记录发生错误!\\n" + e.getMessage());
   return false;
  } catch (IOException e) {
   try {
    conn.rollback();
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
   System.out.println("文件数据读取错误:" + e.getMessage());
   setMessage("对不起,文件数据读取发生错误!\\n" + e.getMessage());
   return false;
  } catch (Exception e) {
   try {
    conn.rollback();
   } catch (Exception e1) {
    e1.printStackTrace();
   }
   System.out.println("未知错误:" + e.getMessage());
   System.out.println("错误时的SQL语句为: " + sql.toString());
   System.out.println("错误的数据为: " + data);
   setMessage("对不起,发生未知错误!\\n" + e.getMessage());
   return false;
  } finally {
   try {
    if (rs != null) {
     rs.close();
    }
   } catch (Throwable e) {
    System.out.println("关闭rs对象时错误:" + e.getMessage());
   }
   try {
    if (pre != null) {
     pre.close();
    }
   } catch (SQLException e) {
    System.out.println("关闭prepareStatement对象时错误:" + e.getMessage());
   }
   try {
    if (conn != null) {
     conn.setAutoCommit(true);
    }
   } catch (SQLException e) {
    System.out.println("恢复Connection对象时错误:" + e.getMessage());
   }
   try {
    if (conn != null) {
     conn.close();
    }
   } catch (SQLException e) {
    System.out.println("关闭Connection对象时错误:" + e.getMessage());
   }
  }
 }


 在这两块都结束后,便完成了对blob字段数据库的存放。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要更新 Oracle 数据BLOB 字段,你可以使用以下步骤: 1. 首先,创建一个包含 BLOB 字段数据表或在已有的表添加 BLOB 字段。 ```sql CREATE TABLE my_table ( id NUMBER, blob_column BLOB ); ``` 2. 使用 UPDATE 语句更新 BLOB 字段的值。你可以使用 PL/SQL 块或 SQL 语句执行此操作。 使用 PL/SQL 块的示例: ```sql DECLARE l_blob BLOB; BEGIN -- 从文件读取新的 BLOB 数据 SELECT empty_blob() INTO l_blob FROM dual; INSERT INTO my_table (id, blob_column) VALUES (1, l_blob); -- 打开 BLOB 对象以进行写入操作 DBMS_LOB.OPEN(l_blob, DBMS_LOB.LOB_READWRITE); -- 写入新的 BLOB 数据 DBMS_LOB.WRITE(l_blob, LENGTHB('New BLOB data'), 1, 'New BLOB data'); -- 关闭 BLOB 对象 DBMS_LOB.CLOSE(l_blob); -- 更新表的记录 UPDATE my_table SET blob_column = l_blob WHERE id = 1; COMMIT; END; / ``` 使用 SQL 语句的示例: ```sql UPDATE my_table SET blob_column = ( SELECT empty_blob() FROM dual ) WHERE id = 1; DECLARE l_blob BLOB; BEGIN SELECT blob_column INTO l_blob FROM my_table WHERE id = 1 FOR UPDATE; -- 打开 BLOB 对象以进行写入操作 DBMS_LOB.OPEN(l_blob, DBMS_LOB.LOB_READWRITE); -- 写入新的 BLOB 数据 DBMS_LOB.WRITE(l_blob, LENGTHB('New BLOB data'), 1, 'New BLOB data'); -- 关闭 BLOB 对象 DBMS_LOB.CLOSE(l_blob); COMMIT; END; / ``` 请注意,这只是一个简单的示例,实际情况可能更复杂。你可能需要根据你的具体需求进行相应的调整。同时,确保在操作 BLOB 字段时进行适当的事务管理和异常处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值