JAVA

          人生若只如初見,何事秋風(fēng)悲畫扇。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            50 隨筆 :: 25 文章 :: 157 評(píng)論 :: 0 Trackbacks

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

          1:存放photo,比如gif的二進(jìn)制數(shù)據(jù):

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



          //再將其寫到內(nèi)存來
          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:對(duì)如比較大的文件可以存在CLOG中:
          oracle.jdbc.OraclePreparedStatement pstmt = 
                (oracle.jdbc.OraclePreparedStatement)OCon.prepareCall(updatesql);
               
          pstmt.setCLOB(
          1,tempclob);//tempclob就是存信DB中clog的內(nèi)容,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;
           }



          /**
          *在此多說幾句呢,在運(yùn)行到
          *tempClob = CLOB.createTemporary(conn,true,CLOB.DURATION_SESSION);
          *時(shí)若是出現(xiàn):ClassCastException 不太受歡迎的信息,看一下你的DB連接是不是連接池什么的,
          *我以前的用的連接(會(huì)出現(xiàn)此異常)如下:
          *<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了(可能是連接池的一個(gè)缺陷吧,呵呵不是太懂)
          */

          將其從DB中寫入到內(nèi)存
          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);
           }

          }


             .

           

          對(duì)于clob我也是在摸索中寫進(jìn)去的(運(yùn)行時(shí)通過),不足之處請(qǐng)指正。謝謝!
          posted on 2006-03-05 11:49 Jkallen 閱讀(1977) 評(píng)論(0)  編輯  收藏 所屬分類: JEE學(xué)習(xí)DB學(xué)習(xí)
          主站蜘蛛池模板: 长垣县| 延安市| 钟祥市| 修文县| 鄂托克前旗| 夏津县| 成武县| 二手房| 新晃| 崇明县| 建阳市| 柏乡县| 合江县| 台中县| 商城县| 砚山县| 庆阳市| 常熟市| 河东区| 新巴尔虎左旗| 康定县| 冕宁县| 株洲县| 平邑县| 浮梁县| 长兴县| 晋城| 镇原县| 曲松县| 武平县| 深水埗区| 沙雅县| 沈丘县| 博湖县| 南川市| 贵州省| 繁峙县| 确山县| 西峡县| 建水县| 榕江县|