在使用Java访问Oracle的Clob或Blob字段时,大家常常会碰到很多问题,利用Oracle的新的更新方式可以解决
/*
* Oracle 测试表
*/
CREATE TABLE T_Demo(
id integer primary key,
content clob
);
/*
* 实体Bean
*/
package com.utsun.common.demo;
public class DemoBean{
private int id;
private String content;
public int getId(){
return this.id;
}
public String getConent(){
return this.content;
}
public void setId(int id){
this.id = id;
}
public void setContent(String content){
this.content = content;
}
}
/*
* Action Bean
*/
package com.utsun.common.demo;
import java.sql.*;
import java.text.SimpleDateFormat;
import tellhow.commonweb.db.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
import java.io.*;
public class DemoActionBean{
DBConnectionManager dbcm = DBConnectionManager.getInstance();
Connection connection = null;
DBAccess dba = null;
void init() throws Exception{
try{
connection = dbcm.getConnection();
dba = new DBAccess(connection);
}
catch (Exception s){
dispose();
throw e;
}
}
void dispose() throws Exception{
try{
dbcm.freeConnection(connection);
}
catch (Exception e){
throw e;
}
}
public BulletinBean fetch(int keyid) throws Exception{
DemoBean bean = new DemoBean();
bean.setBtID(keyid);
String sql = "SELECT * FROM T_Demo WHERE id=" + Integer.toString(keyid);
String line = "";
String text = "";
ResultSet rs = null;
try{
init();
rs = dba.openSelect(sql);
try{
while (rs.next()){
CLOB clob = (oracle.sql.CLOB)rs.getClob("Content");
if (clob != null){
BufferedReader in = new BufferedReader(clob.getCharacterStream());
while ((line = in.readLine()) != null)
text += line + "\n";
in.close();
}
bean.setContent(text);
break;
}
}
finally{
rs.close();
}
}
catch (Exception e){
throw e;
}
finally{
dispose();
}
return bean;
}
public boolean insert(DemoBean bean) throws Exception {
boolean result = true;
try {
init();
int maxID = 1;
ResultSet rs = dba.openSelect("SELECT NVL(Max(id),0) FROM T_Demo");
while (rs.next())
maxID = rs.getInt(1) + 1;
rs.close();
StringBuffer sqlBT = new StringBuffer();
sqlBT.append("BEGIN ");
sqlBT.append(" INSERT INTO T_Demo (id, content) ");
sqlBT.append(" VALUES (?,EMPTY_CLOB())");
sqlBT.append(" RETURN content INTO ?; ");
sqlBT.append("END;");
connection.setAutoCommit(false);
OracleCallableStatement ocs = (OracleCallableStatement)connection.prepareCall(sqlBT.toString());
ocs.setInt(1, maxID);
ocs.registerOutParameter(2, OracleTypes.CLOB);
ocs.executeUpdate();
CLOB clob = ocs.getCLOB(2);
clob.putString(1, bean.getContent());
ocs.close();
connection.commit();
}
catch (Exception e) {
connection.rollback();
throw e;
}
finally {
dispose();
}
return result;
}
public boolean update(DemoBean bean) throws Exception {
boolean result = true;
try {
init();
StringBuffer sqlBT = new StringBuffer();
sqlBT.append("BEGIN ");
sqlBT.append(" UPDATE T_Demo SET content=EMPTY_CLOB() WHERE id=? ");
sqlBT.append(" RETURN content INTO ?; ");
sqlBT.append("END;");
connection.setAutoCommit(false);
OracleCallableStatement ocsBT = (OracleCallableStatement)connection.prepareCall(sqlBT.toString());
ocsBT.setInt(2, bean.getID());
ocsBT.registerOutParameter(1, OracleTypes.CLOB);
ocsBT.executeUpdate();
CLOB clob = ocsBT.getCLOB(1);
clob.putString(1, content);
ocsBT.close();
connection.commit();
}
catch (Exception e) {
......
浙公网安备 33010602011771号