当前位置: 技术问答>java相关
clob存储异常,请大家帮忙看看有什么错误?
来源: 互联网 发布时间:2015-07-16
本文导语: 以下代码更新数据库的clob字段,但是运行时总发生如下异常: java.sql.SQLException: ORA-22292: 无法在没有事务处理的情况下以读写模式打开 LOB ORA-06512: 在"SYS.DBMS_LOB", line 599 ORA-06512: 在line 1 代码如下: public static vo...
以下代码更新数据库的clob字段,但是运行时总发生如下异常:
java.sql.SQLException: ORA-22292: 无法在没有事务处理的情况下以读写模式打开 LOB
ORA-06512: 在"SYS.DBMS_LOB", line 599
ORA-06512: 在line 1
代码如下:
public static void setClob(String strSql,String lob_fieldName,String strValue) throws Exception {
PreparedStatement stmt = null;
OracleCallableStatement cstmt=null;
ResultSet rs = null;
Writer writer;
DBConnection conn=new DBConnection();
try{
conn.beginTrans();
stmt = conn.getPreparedStmt(strSql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery();
if (rs.next()) {
try{
CLOB clob =(CLOB) rs.getClob(lob_fieldName);
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN COMMIT;END;"));
cstmt.execute();
//调用oracle dbms_lob包以读写模式打开CLOB
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
cstmt.setCLOB(1,clob);
cstmt.execute();
//裁减原有lob字段长度为零
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.TRIM(?,0);END;"));
cstmt.setCLOB(1,clob);
cstmt.execute();
//关闭CLOB
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.CLOSE(?);END;"));
cstmt.setCLOB(1,clob);
cstmt.execute();
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN COMMIT;END;"));
cstmt.execute();
writer=clob.getCharacterOutputStream();
writer.write(strValue);
writer.flush();
writer.close();
conn.commit();
}
catch(Exception e) {
conn.rollback();
System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
throw e;
}
}
else {
conn.rollback();
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
throw e;
}
}
java.sql.SQLException: ORA-22292: 无法在没有事务处理的情况下以读写模式打开 LOB
ORA-06512: 在"SYS.DBMS_LOB", line 599
ORA-06512: 在line 1
代码如下:
public static void setClob(String strSql,String lob_fieldName,String strValue) throws Exception {
PreparedStatement stmt = null;
OracleCallableStatement cstmt=null;
ResultSet rs = null;
Writer writer;
DBConnection conn=new DBConnection();
try{
conn.beginTrans();
stmt = conn.getPreparedStmt(strSql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery();
if (rs.next()) {
try{
CLOB clob =(CLOB) rs.getClob(lob_fieldName);
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN COMMIT;END;"));
cstmt.execute();
//调用oracle dbms_lob包以读写模式打开CLOB
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
cstmt.setCLOB(1,clob);
cstmt.execute();
//裁减原有lob字段长度为零
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.TRIM(?,0);END;"));
cstmt.setCLOB(1,clob);
cstmt.execute();
//关闭CLOB
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.CLOSE(?);END;"));
cstmt.setCLOB(1,clob);
cstmt.execute();
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN COMMIT;END;"));
cstmt.execute();
writer=clob.getCharacterOutputStream();
writer.write(strValue);
writer.flush();
writer.close();
conn.commit();
}
catch(Exception e) {
conn.rollback();
System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
throw e;
}
}
else {
conn.rollback();
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException e){
System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
throw e;
}
}
|
conn.beginTrans();改成
conn.setAutoCommit(true);
在下面这句前加上
conn.beginTrans();
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
conn.commit();
conn.setAutoCommit(true);
在下面这句前加上
conn.beginTrans();
cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
conn.commit();
|
这是一段关于LOB字段的程序,希望对你有用
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.io.*;
//import javax.sql.*;
public class blobtest
{
public static void main(String[] args)
{
BLOB bloba;
Connection con = null;
// PreparedStatement pstmt = null;
ResultSet rs;
String sQueryP = "SELECT TCBlob FROM TeeColor";
try // Attempt to load the JDBC driver
{ // with newInstance
Class.forName( "oracle.jdbc.driver.OracleDriver" ).newInstance();
}
catch( Exception e ) // error
{
System.err.println("Failed to load current driver.");
return;
} // end catch
try
{
con = DriverManager.getConnection ( "jdbc:oracle:thin:@fzw:1521:orcl",
"oa",
"fcgoamain");
Statement stmt = con.createStatement ();
rs = stmt.executeQuery( sQueryP );
}
catch ( Exception e)
{
System.err.println( "problems connecting to 1 :" );
System.err.println( e.getMessage() );
if( con != null)
{
try { con.close(); }
catch( Exception e2 ) {}
}
return;
} // end catch
try
{
//rs = pstmt.executeQuery();
while( rs.next() )
{
System.out.println("select blob");
bloba =(BLOB)rs.getObject( 1 );
System.out.println("select blob success");
byte[] byte_a=new byte[(int)bloba.length()];
InputStream r_stream=bloba.getBinaryStream();
for(int i=0;i
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.io.*;
//import javax.sql.*;
public class blobtest
{
public static void main(String[] args)
{
BLOB bloba;
Connection con = null;
// PreparedStatement pstmt = null;
ResultSet rs;
String sQueryP = "SELECT TCBlob FROM TeeColor";
try // Attempt to load the JDBC driver
{ // with newInstance
Class.forName( "oracle.jdbc.driver.OracleDriver" ).newInstance();
}
catch( Exception e ) // error
{
System.err.println("Failed to load current driver.");
return;
} // end catch
try
{
con = DriverManager.getConnection ( "jdbc:oracle:thin:@fzw:1521:orcl",
"oa",
"fcgoamain");
Statement stmt = con.createStatement ();
rs = stmt.executeQuery( sQueryP );
}
catch ( Exception e)
{
System.err.println( "problems connecting to 1 :" );
System.err.println( e.getMessage() );
if( con != null)
{
try { con.close(); }
catch( Exception e2 ) {}
}
return;
} // end catch
try
{
//rs = pstmt.executeQuery();
while( rs.next() )
{
System.out.println("select blob");
bloba =(BLOB)rs.getObject( 1 );
System.out.println("select blob success");
byte[] byte_a=new byte[(int)bloba.length()];
InputStream r_stream=bloba.getBinaryStream();
for(int i=0;i