JSP Oralcle CLOB 文件保存

来源:互联网 发布:淘宝 强光手电筒 编辑:程序博客网 时间:2024/05/23 17:54
  1. <%@ page import="java.io.*"%> 
  2. <%@ page import="oracle.sql.CLOB"%> 
  3. <%
  4. try
  5. {
  6.     Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
  7.     int imgId=23;
  8.     String url="jdbc:oracle:thin:@localhost:1521:ORCL";
  9.     Connection conn=DriverManager.getConnection(url,"system","oracle");
  10.     Statement st = conn.createStatement();
  11.     st.executeQuery("insert into hr.psn values("+ imgId +",'wenzhixing',EMPTY_CLOB())");
  12.     ResultSet rs = st.executeQuery("select filestr from hr.psn where id="+ imgId +" for update");
  13.     conn.setAutoCommit(false); 
  14.     if (rs.next())
  15.     {
  16.          /* 取出此CLOB对象 */
  17.         oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("filestr");
  18.         /* 向CLOB对象中写入数据 */
  19.         Writer outStream = clob.getCharacterOutputStream();
  20.         // 这里用一个文件模拟输入流
  21.         FileReader in = new FileReader(new File("f://2.mht"));///////本地的文件位置
  22.         char[] b = new char[clob.getBufferSize()];
  23.         int len = -1;
  24.         while ((len = in.read(b)) != -1)
  25.         {
  26.             outStream.write(b, 0, len);
  27.         }
  28.         in.close();
  29.         outStream.close();
  30.     }
  31.     conn.commit();
  32.     rs.close();
  33.     st.close();
  34.     conn.close();
  35. }
  36. catch (Exception e)
  37. {
  38.     e.printStackTrace();
  39. }
  40. %>