二十二樓Blog遷移 2007-03

          數據庫: 兩種連接數據庫
          ??? //通過JDBC獲得數據庫連接
          ??? Class.forName("oracle.jdbc.driver.OracleDriver");
          ??? Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
          ??? con.setAutoCommit(false);
          ??? Statement st = con.createStatement();
          ??? //插入一個空對象empty_blob()
          ??? st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
          ??? //鎖定數據行進行更新,注意“for update”語句
          ??? ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
          ??? if (rs.next())
          ??? {
          ??????? //得到java.sql.Blob對象后強制轉換為oracle.sql.BLOB
          ??????? oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");
          ??????? OutputStream outStream = blob.getBinaryOutputStream();
          ??????? //data是傳入的byte數組,定義:byte[] data
          ??????? outStream.write(data, 0, data.length);
          ??? }
          ??? outStream.flush();
          ??? outStream.close();
          ??? con.commit();
          ??? con.close();
          (2)JNDI方式
          ??? //通過JNDI獲得數據庫連接
          ??? Context context = new InitialContext();
          ??? ds = (DataSource) context.lookup("ORA_JNDI");
          ??? Connection con = ds.getConnection();
          ??? con.setAutoCommit(false);
          ??? Statement st = con.createStatement();
          ??? //插入一個空對象empty_blob()
          ??? st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
          ??? //鎖定數據行進行更新,注意“for update”語句
          ??? ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
          ??? if (rs.next())
          ??? {
          ??????? //得到java.sql.Blob對象后強制轉換為weblogic.jdbc.vendor.oracle.OracleThinBlob(不同的App Server對應的可能會不同)
          ??????? weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob("BLOBATTR");
          ??????? OutputStream outStream = blob.getBinaryOutputStream();
          ??????? //data是傳入的byte數組,定義:byte[] data
          ??????? outStream.write(data, 0, data.length);
          ??? }
          ??? outStream.flush();
          ??? outStream.close();
          ??? con.commit();
          ??? con.close();
          zqbchina 發表于: 2007-03-18 18:10:45

          posted on 2007-08-02 17:33 心硯 閱讀(332) 評論(0)  編輯  收藏 所屬分類: 生活

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          文章分類

          文章檔案

          Forum

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 靖边县| 红安县| 抚顺县| 宣武区| 宁海县| 大余县| 久治县| 将乐县| 海淀区| 东丰县| 桐梓县| 乐东| 新巴尔虎右旗| 宿迁市| 石台县| 廉江市| 美姑县| 隆安县| 来宾市| 平顶山市| 长沙市| 婺源县| 五河县| 滦南县| 神木县| 长岛县| 福鼎市| 斗六市| 莱阳市| 绩溪县| 乌苏市| 闽清县| 潞西市| 乌兰县| 象山县| 天台县| 雅江县| 阿合奇县| 宣城市| 卢氏县| 平安县|