JAVA

          人生若只如初見,何事秋風悲畫扇。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            50 隨筆 :: 25 文章 :: 157 評論 :: 0 Trackbacks

          在持久化過程中我們往往要用到存儲一些大數據的操作,比如說用戶的靚照,還有用戶的整個數據文件什么的啦!在oracle中提供了blog,clog等字段好象可以提供此功能呢(另外當然也可以直接放到如硬盤等其它存儲設備上)。
          就我個的人一些實踐記下:

          1:存放photo,比如gif的二進制數據:

          //在一個字節數據中存放此gif文件的二進制數據
          InputStream in = new InputStream("photo.gif");//locate你的photho啦!
          byte[] buffer_in = new byte[in.available()];
          in.read(buffer_in);
          in.close();
          //將buffer_in持久化到DB中去



          //再將其寫到內存來
          byte[] buffer_out = getPhoto();//get the buufer_in from DB  ^_^
          String path = context.getRealPath("/");
          FileOutputStream fout 
          = new FileOutputStream(path+"photo.gif");//locate your out path for your photo  
          fout.write(buffer_out);
          fout.close();

          2:對如比較大的文件可以存在CLOG中:
          oracle.jdbc.OraclePreparedStatement pstmt = 
                (oracle.jdbc.OraclePreparedStatement)OCon.prepareCall(updatesql);
               
          pstmt.setCLOB(
          1,tempclob);//tempclob就是存信DB中clog的內容,clog的得到可以由以下function。


          //得到存于oracle表中clob的function
          private CLOB getCLOB(String xmlData, Connection conn)
            
          throws SQLException
           
          {
            CLOB tempClob 
          = null;
            
          try
            
          {
             
          //tempClob = CLOB.getEmptyCLOB();
             tempClob = CLOB.createTemporary(conn,true,CLOB.DURATION_SESSION);
             
          // If the temporary CLOB has not yet been created, create one
            
          // tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);
             
          // Open the temporary CLOB in readwrite mode, to enable writing
             tempClob.open(CLOB.MODE_READWRITE);
             
          // Get the output stream to write
             Writer tempClobWriter = tempClob.getCharacterOutputStream();
             
          // Write the data into the temporary CLOB
             tempClobWriter.write(xmlData);
             
          // Flush and close the stream
             tempClobWriter.flush();
             tempClobWriter.close();
             
          // Close the temporary CLOB
             tempClob.close();
            }

            
          catch(SQLException sqlexp)
            
          {
             tempClob.freeTemporary();
             sqlexp.printStackTrace();
            }

            
          catch(Exception exp)
            
          {
             tempClob.freeTemporary();
             exp.printStackTrace();
            }

            
          return tempClob;
           }



          /**
          *在此多說幾句呢,在運行到
          *tempClob = CLOB.createTemporary(conn,true,CLOB.DURATION_SESSION);
          *時若是出現:ClassCastException 不太受歡迎的信息,看一下你的DB連接是不是連接池什么的,
          *我以前的用的連接(會出現此異常)如下:
          *<Resource name="jdbc/ttt" auth="Container" type="javax.sql.DataSource" 
                         maxActive="100" maxIdle="30" maxWait="3000"
                         username="LMSOwner" password="password" driverClassName="oracle.jdbc.driver.OracleDriver"
                         url="jdbc:oracle:thin:@appserver:1521:ttt"/>

           

           try
                {
                   if ( _Debug )
                   {
                      System.out.println("  ::--> Connecting to the DB");
                   }

            Context initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            DataSource ds = (DataSource) envContext.lookup("jdbc/ttt");
            conn = ds.getConnection();
                }

           

          改成:
            


          try{
          //   Load the database details into the variables.
            String url="jdbc:oracle:thin:@appserver:1521:ttt";
            String user     = "LMSOwner";
            String password = "ttt";

          //   Create the properties object that holds all database details
            java.util.Properties props = new java.util.Properties();
            props.put("user", user );
            props.put("password", password);
            props.put("SetBigStringTryClob", "true");

          //   Load the Oracle JDBC driver class.
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());     

          //   Get the database connection 
            conn = DriverManager.getConnection( url, props );
            }


           

          就OK了(可能是連接池的一個缺陷吧,呵呵不是太懂)
          */

          將其從DB中寫入到內存
          import java.io.IOException;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          //to connect DB
          import com.studyez.lms.util.LMSDatabaseHandler;
          import java.sql.*;

          import oracle.sql.*;

          import oracle.xdb.XMLType;

          /**
           * Servlet implementation class for Servlet: UserCourseData
           * 
           
          */

          public class UserCourseData extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
          {
           
          /**
            * 
            
          */

           
          private static final long serialVersionUID = 1L;

           
          /*
            * (non-Java-doc)
            * 
            * @see javax.servlet.http.HttpServlet#HttpServlet()
            
          */

           
          public UserCourseData()
           
          {
            
          super();
           }


           
          /*
            * (non-Java-doc)
            * 
            * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
            *      HttpServletResponse response)
            
          */

           
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
            
          throws ServletException, IOException
           
          {
            
          // TODO Auto-generated method stub
            String tlearnerID = (String) request.getParameter("learnerID");
            
          int lID = Integer.parseInt(tlearnerID);

            String courseID 
          = (String) request.getParameter("courseID");
            
          int cID = Integer.parseInt(courseID);

            String sql 
          = "select * from usercoursedata  where userID = " + lID + " and courseID = " + cID;

            String tempDocStr 
          = null;
            
          try
            
          {
             Connection conn 
          = LMSDatabaseHandler.getTempConn();
             Statement stmt 
          = conn.createStatement();
             ResultSet rst 
          = stmt.executeQuery(sql);
             OPAQUE xml;

             
          if(rst.next())
             
          {
              
          // rst.first();
              oracle.jdbc.OracleResultSet temprst = (oracle.jdbc.OracleResultSet) rst;

              xml 
          = temprst.getOPAQUE("xmlDoc");
              XMLType xt 
          = XMLType.createXML(xml);
             
          // doc = xt.getDOM();
              tempDocStr = xt.getStringVal();

            
          //  System.out.println("Testing getDOM() ");

             }

             
          else
             
          {
              System.out.println(
          "shit he is go now ");
              tempDocStr 
          = "<root></root>";
             }

             
          // Write XML to response.
             response.setContentType("application/xml");
             response.getWriter().write(tempDocStr);

             
          if(rst != null)
              rst.close();
             
          if(conn != null)
              conn.close();
             
            
          // System.out.println(tempDocStr);
            }

            
          catch(SQLException e)
            
          {
             e.printStackTrace();
             tempDocStr 
          = "<root></root>";

             
          // Write XML to response.
             response.setContentType("application/xml");
             response.getWriter().write(tempDocStr);
            }


           }


           
          /*
            * (non-Java-doc)
            * 
            * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
            *      HttpServletResponse response)
            
          */

           
          protected void doPost(HttpServletRequest request, HttpServletResponse response)
            
          throws ServletException, IOException
           
          {
            
          // TODO Auto-generated method stub
            doGet(request, response);
           }

          }


             .

           

          對于clob我也是在摸索中寫進去的(運行時通過),不足之處請指正。謝謝!
          posted on 2006-03-05 11:49 Jkallen 閱讀(1972) 評論(0)  編輯  收藏 所屬分類: JEE學習DB學習
          主站蜘蛛池模板: 永顺县| 镇原县| 涡阳县| 文成县| 红原县| 涞水县| 浦东新区| 伊宁市| 青浦区| 辽阳县| 永城市| 四川省| 菏泽市| 库伦旗| 广西| 都匀市| 石屏县| 泰来县| 博湖县| 凌云县| 罗源县| 哈巴河县| 沂南县| 建水县| 原平市| 淮阳县| 柳林县| 宁阳县| 安岳县| 资溪县| 松溪县| 连州市| 张家川| 焉耆| 正宁县| 抚顺市| 南靖县| 商都县| 博乐市| 太白县| 巨野县|