<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    鷹翔宇空

    學習和生活

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      110 Posts :: 141 Stories :: 315 Comments :: 1 Trackbacks

    這是一個完整的使用java對oracle9i的操作,其實,使用oracle10g后,操作將非常方便,但現在由于仍在使用oracle9i,所以不得不忍受這種痛苦。現將對oracle9i中CLOB類型的操作,完整記錄于此,權作自己的學習筆記吧。

    第一種情況:沒有服務器,單獨使用java直接連接數據庫時:

    package com.hyq.test;

    import java.sql.SQLException;
    import java.sql.*;

    import java.sql.ResultSet;
    import oracle.sql.CLOB;

    public class ManageOracleCLOB {
    ? public ManageOracleCLOB() {
    ? }

    ? public static void main(String[] args) {

    ??? String driver = "oracle.jdbc.driver.OracleDriver";

    ??? String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521: HYQ";

    ??? Statement stmt = null;

    ??? ResultSet rs = null;

    ??? Connection conn = null;
    ???
    ??? ManageOracleCLOB manageOracleCLOB = null;

    ??? try {

    ????? Class.forName(driver);

    ????? DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    ????? conn = DriverManager.getConnection(strUrl, "HYQ", "HYQ");
    ?????
    ????? manageOracleCLOB = new ManageOracleCLOB();
    ????? //獲取下一個標識符id
    ????? long nextId = manageOracleCLOB.getNextId(conn);
    //????? long nextId = 1;
    ????? //添加記錄
    ????? manageOracleCLOB.add(conn,nextId);
    ????? //修改記錄
    //????? manageOracleCLOB.modify(conn,nextId);
    ????? //獲取記錄
    //????? manageOracleCLOB.find(conn,nextId);
    ????? //刪除記錄
    //????? manageOracleCLOB.delete(conn,nextId);
    ?????
    ????? conn.close();
    ??? }

    ??? catch (SQLException ex2) {

    ????? ex2.printStackTrace();

    ??? }

    ??? catch (Exception ex2) {

    ????? ex2.printStackTrace();

    ??? }
    ??? finally {

    ????? try {

    ??????? if (rs != null) {

    ????????? rs.close();

    ????????? if (stmt != null) {

    ??????????? stmt.close();

    ????????? }

    ????????? if (conn != null) {

    ??????????? conn.close();

    ????????? }

    ??????? }

    ????? }

    ????? catch (SQLException ex1) {

    ????? }

    ??? }

    ? }

    ? /**
    ?? * add
    ?? *
    ?? * @param conn Connection
    ?? * @param hyq long
    ?? */
    ? public void add(Connection conn, long hyq) {
    ??? CLOB ad = null;
    ??? PreparedStatement pstmt = null;
    ??? try {
    ????? conn.setAutoCommit(false);

    ????? pstmt = conn.prepareStatement("INSERT INTO S_PROCLAIM_TEST (PROCLAIM_ID,PROCLAIM_TITLE,PROCLAIM_CONTENT) VALUES (?,?,'" +
    ??????????????????????????????????? CLOB.empty_lob() + "')");

    ????? pstmt.setLong(1, hyq);
    ????? pstmt.setString(2, "第一次的嘗試!");

    ????? pstmt.executeUpdate();

    ????? pstmt.close();

    ????? pstmt = conn.prepareStatement(
    ????????? "select PROCLAIM_CONTENT from S_PROCLAIM_TEST where PROCLAIM_ID= ? for update");

    ????? pstmt.setLong(1, hyq);

    ????? ResultSet rset = pstmt.executeQuery();
    ????? if (rset.next()) {
    ??????? ad = (CLOB) rset.getClob(1);
    ????? }
    ????? pstmt = conn.prepareStatement(
    ????????? "UPDATE S_PROCLAIM_TEST SET PROCLAIM_CONTENT=? WHERE PROCLAIM_ID=?");
    ????? String hyqTa = "hyq冬季到臺北來看雨,別在異鄉哭泣。也許有一天上天睜開了眼,看到你哭泣的臉和早已凋謝了的容顏,本想安慰你傷痛的內心卻忍不住淚流滿面。你是風兒它是沙,瘋瘋癲癲到天涯。";
    ????? ad.putString(1, hyqTa);

    ????? pstmt.setClob(1, ad);
    ????? pstmt.setLong(2, hyq);

    ????? pstmt.executeUpdate();
    ????? pstmt.close();

    ????? conn.commit();
    ????? ad = null;
    ??? }
    ??? catch (SQLException ex) {
    ????? ex.printStackTrace();
    ??? }
    ??? finally {
    ????? if (pstmt != null) {
    ??????? try {
    ????????? pstmt.close();
    ??????? }
    ??????? catch (SQLException ex1) {
    ??????? }
    ??????? pstmt = null;
    ????? }
    ??? }

    ? }

    ? /**
    ?? * modify
    ?? *
    ?? * @param conn Connection
    ?? * @param hyq long
    ?? */
    ? public void modify(Connection conn, long hyq) {
    ??? CLOB ad = null;
    ??? PreparedStatement pstmt = null;
    ??? try {
    ?????
    ????? conn.setAutoCommit(false);
    ?????
    ????? pstmt = conn.prepareStatement(
    ????????? "select PROCLAIM_CONTENT from S_PROCLAIM_TEST where PROCLAIM_ID= ? for update");

    ????? pstmt.setLong(1, hyq);

    ????? ResultSet rset = pstmt.executeQuery();
    ????? if (rset.next()) {
    ??????? ad = (CLOB) rset.getClob(1);
    ????? }
    ????? pstmt.close();
    ????? pstmt = conn.prepareStatement(
    ????????? "UPDATE S_PROCLAIM_TEST SET PROCLAIM_CONTENT=? WHERE PROCLAIM_ID=?");
    ????? String hyqTa = "這兩天做一個小東西,要求將一個文件打包到.war文件中,然后還要將這個文件從.war包中讀取出來,并在服務器硬盤上重新建一個新的文件。本來很簡單的東西,卻浪費了不少時間,寫出來,做一下筆記,同時給那些需要的朋友提供一下參考,下面是我寫的一個示例,在servlet中寫的";
    ????? ad.putString(1, hyqTa);

    ????? pstmt.setClob(1, ad);
    ????? pstmt.setLong(2, hyq);

    ????? pstmt.executeUpdate();
    ????? pstmt.close();

    ????? conn.commit();
    ????? ad = null;
    ??? }
    ??? catch (SQLException ex) {
    ????? ex.printStackTrace();
    ??? }
    ??? finally {
    ????? if (pstmt != null) {
    ??????? try {
    ????????? pstmt.close();
    ??????? }
    ??????? catch (SQLException ex1) {
    ??????? }
    ??????? pstmt = null;
    ????? }
    ??? }
    ? }

    ? /**
    ?? * find
    ?? *
    ?? * @param conn Connection
    ?? * @param hyq long
    ?? */
    ? public void find(Connection conn, long hyq) {
    ??? Clob aa = null;
    ??? PreparedStatement pstmt = null;
    ??? try {
    ????? pstmt = conn.prepareStatement(
    ????????? "select PROCLAIM_CONTENT from S_PROCLAIM_TEST where PROCLAIM_ID= ?");
    ????? pstmt.setLong(1, hyq);
    ????? ResultSet rset2 = pstmt.executeQuery();
    ????? if (rset2.next()) {
    ??????? aa = rset2.getClob(1);
    ????? }
    ????? conn.commit();
    ????? String content = aa.getSubString(1, (int) aa.length());
    ????? System.out.println("==hyq==getContent===" + content);
    ??? }
    ??? catch (SQLException ex) {
    ????? ex.printStackTrace();
    ??? }
    ??? finally {
    ????? if (pstmt != null) {
    ??????? try {
    ????????? pstmt.close();
    ??????? }
    ??????? catch (SQLException ex1) {
    ??????? }
    ??????? pstmt = null;
    ????? }
    ??? }
    ? }

    ? /**
    ?? * delete
    ?? *
    ?? * @param conn Connection
    ?? * @param hyq long
    ?? */
    ? public void delete(Connection conn, long hyq) {
    ??? PreparedStatement pstmt = null;
    ??? try {
    ????? pstmt = conn.prepareStatement(
    ????????? "DELETE FROM S_PROCLAIM_TEST WHERE PROCLAIM_ID = ?");
    ????? pstmt.setLong(1, hyq);
    ????? pstmt.executeQuery();
    ??? }
    ??? catch (SQLException ex) {
    ????? ex.printStackTrace();
    ??? }
    ??? finally {
    ????? if (pstmt != null) {
    ??????? try {
    ????????? pstmt.close();
    ??????? }
    ??????? catch (SQLException ex1) {
    ??????? }
    ??????? pstmt = null;
    ????? }
    ??? }
    ? }

    ? /**
    ?? * getNextId
    ?? *
    ?? * @param conn Connection
    ?? * @return long
    ?? */
    ? public long getNextId(Connection conn) {
    ??? long nextProclaimId = 0;
    ??? PreparedStatement pstmt = null;
    ??? ResultSet rs = null;
    ??? String sqlStr = "SELECT MAX(PROCLAIM_ID) FROM? S_PROCLAIM_TEST";
    ??? try {
    ????? try {
    ??????? pstmt = conn.prepareStatement(sqlStr);
    ??????? rs = pstmt.executeQuery();
    ??????? if (rs != null && rs.next()) {
    ????????? if (rs.getString(1) != null) {
    ??????????? nextProclaimId = Long.parseLong(rs.getString(1))+1;
    ????????? }
    ????????? else {
    ??????????? nextProclaimId = 1;
    ????????? }
    ??????? }
    ??????? else {
    ????????? nextProclaimId = 1;
    ??????? }
    ????? }
    ????? catch (SQLException ex) {
    ??????? ex.printStackTrace();
    ????? }
    ??? }
    ??? finally {
    ????? if (rs != null) {
    ??????? try {
    ????????? rs.close();
    ??????? }
    ??????? catch (SQLException ex1) {
    ??????? }
    ??????? rs = null;
    ????? }
    ????? if (pstmt != null) {
    ??????? try {
    ????????? pstmt.close();
    ??????? }
    ??????? catch (SQLException ex2) {
    ??????? }
    ??????? pstmt = null;
    ????? }
    ??? }
    ??? return nextProclaimId;
    ? }

    }

    第二種:使用weblogic服務器時出現的問題

    使用webligic時,在上面的方法中(CLOB) rset.getClob(1);這一行會報錯java.lang.ClassCastException??,說是類型轉換錯誤。很郁悶。修改結果是,去掉CLOB類型,使用weblogic自帶的weblogic.jar(可以在weblojic的安裝目錄下找到),使用weblogic的weblogic.jdbc.vendor.oracle.OracleThinClob。代碼如下:

    weblogic.jdbc.vendor.oracle.OracleThinClob clob = null;

    ad= (OracleThinClob)tempRset.getClob(1);//替換掉ad = (CLOB) rset.getClob(1);

    然后添加時還要進行類型轉換:pstmt.setClob(1,(Clob)ad);

    這樣一切就OK了!

    真是麻煩之極!

    第三種:使用tomcat連接池的問題

    在使用jbuilder編譯時,會出現一個classes12.jar,把lib下的classes12.jar(oracle包)刪除就可以了。

    posted on 2006-05-16 09:57 TrampEagle 閱讀(2447) 評論(1)  編輯  收藏 所屬分類: 學習體會

    Feedback

    # re: java對oracle9i中CLOB類型的操作 2011-05-04 14:58 亮亮
    在更新CLOB字段類型的值時,為什么在原來的基礎上增加文字時,可以修改成功。但是在原來的基礎上減少文字時,修改是成功了,但是字段值卻并沒有被修改掉呢?  回復  更多評論
      

    主站蜘蛛池模板: 水蜜桃亚洲一二三四在线| 亚洲国产精品综合久久网络| 亚洲最大的成网4438| 91精品全国免费观看青青| 亚洲人成网7777777国产| 天堂在线免费观看| 亚洲成年轻人电影网站www | 人妻无码一区二区三区免费| 亚洲乱亚洲乱妇无码| 中国在线观看免费国语版| 亚洲一线产区二线产区区| 久久久久国产精品免费网站| 亚洲人成人一区二区三区| 无码日韩精品一区二区免费暖暖| 婷婷亚洲久悠悠色悠在线播放| 久久精品免费一区二区| 亚洲视频在线观看2018| 亚洲国产精品午夜电影| 9久9久女女免费精品视频在线观看| 亚洲精品国产日韩| 国产婷婷高清在线观看免费| jizz免费在线影视观看网站| 亚洲日韩精品A∨片无码| 97国产在线公开免费观看| 中文文字幕文字幕亚洲色| 国产精品黄页在线播放免费| 久久一区二区免费播放| 少妇中文字幕乱码亚洲影视| 免费看国产成年无码AV片| 人妻无码中文字幕免费视频蜜桃| 亚洲乱码中文字幕综合| 亚洲国产精品99久久久久久| 亚洲精品视频在线看| 免费观看91视频| 亚洲一本到无码av中文字幕 | 一区二区三区四区免费视频 | 亚洲国产精品免费视频| 日韩免费在线视频| 在线亚洲高清揄拍自拍一品区| 亚洲爽爽一区二区三区| 亚洲精品免费在线|