這是一個完整的使用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包)刪除就可以了。