二十二樓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

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 华安县| 静安区| 马山县| 安吉县| 五大连池市| 当涂县| 西充县| 响水县| 和田市| 康保县| 汤阴县| 瑞昌市| 安阳市| 利辛县| 修水县| 湘西| 博野县| 深泽县| 谢通门县| 咸阳市| 东兴市| 垦利县| 齐齐哈尔市| 三穗县| 东安县| 抚远县| 沧源| 潮安县| 芜湖县| 罗定市| 云林县| 绩溪县| 清远市| 湾仔区| 潮州市| 嵩明县| 岳普湖县| 丰城市| 旌德县| 宿松县| 淳安县|