JAVA

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            50 隨筆 :: 25 文章 :: 157 評論 :: 0 Trackbacks
          <2006年3月>
          2627281234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          公告

            In life there are very rare chances that you'll meet the person you love and loves you in return. So once you have it don't ever let go, the chance might never come your way.

          常用鏈接

          留言簿(20)

          隨筆分類(55)

          隨筆檔案(50)

          文章分類(2)

          文章檔案(25)

          相冊

          JAVA

          友人鏈接

          實用查詢

          珍藏鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

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

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

          //在一個字節(jié)數(shù)據(jù)中存放此gif文件的二進制數(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:對如比較大的文件可以存在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;
           }



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

          將其從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);
           }

          }


             .

           

          對于clob我也是在摸索中寫進去的(運行時通過),不足之處請指正。謝謝!
          posted on 2006-03-05 11:49 Jkallen 閱讀(1975) 評論(0)  編輯  收藏 所屬分類: JEE學(xué)習(xí)DB學(xué)習(xí)
          主站蜘蛛池模板: 宜章县| 舒城县| 临漳县| 浦东新区| 高密市| 周宁县| 邮箱| 辉南县| 中江县| 丽水市| 日土县| 开鲁县| 易门县| 洞头县| 吉木乃县| 云浮市| 灵台县| 托克逊县| 台山市| 将乐县| 苍溪县| 马龙县| 麻栗坡县| 平利县| 和田市| 大同市| 林周县| 望江县| 佳木斯市| 棋牌| 措勤县| 乐平市| 安岳县| 井研县| 禄劝| 河源市| 沧源| 张北县| 潮州市| 布拖县| 故城县|