鷹翔宇空

          學(xué)習(xí)和生活

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

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

          第一種情況:沒有服務(wù)器,單獨(dú)使用java直接連接數(shù)據(jù)庫時:

          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();
          ????? //獲取下一個標(biāo)識符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冬季到臺北來看雨,別在異鄉(xiāng)哭泣。也許有一天上天睜開了眼,看到你哭泣的臉和早已凋謝了的容顏,本想安慰你傷痛的內(nèi)心卻忍不住淚流滿面。你是風(fēng)兒它是沙,瘋瘋癲癲到天涯。";
          ????? 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包中讀取出來,并在服務(wù)器硬盤上重新建一個新的文件。本來很簡單的東西,卻浪費(fèi)了不少時間,寫出來,做一下筆記,同時給那些需要的朋友提供一下參考,下面是我寫的一個示例,在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服務(wù)器時出現(xiàn)的問題

          使用webligic時,在上面的方法中(CLOB) rset.getClob(1);這一行會報錯java.lang.ClassCastException??,說是類型轉(zhuǎn)換錯誤。很郁悶。修改結(jié)果是,去掉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);

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

          這樣一切就OK了!

          真是麻煩之極!

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

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

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

          Feedback

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

          主站蜘蛛池模板: 江城| 宜兰县| 岚皋县| 北安市| 涟水县| 宜章县| 兴业县| 凤凰县| 休宁县| 吕梁市| 普陀区| 宁陕县| 舞钢市| 顺义区| 三门峡市| 丰顺县| 凤阳县| 德阳市| 乌拉特中旗| 寿阳县| 昌吉市| 黑河市| 宜昌市| 左贡县| 东乌| 彰化县| 乌海市| 舒兰市| 泰兴市| 都昌县| 松滋市| 尉氏县| 南阳市| 蓬莱市| 桓台县| 左云县| 巍山| 饶平县| 罗山县| 阿巴嘎旗| 岫岩|