weblogic、oracle、BLOB的问题

iis40 2002-09-12 01:52:35
把大对象寸入oracle数据库的blob的字段,其中有这么一句语句:

oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("doccontent")

也就是把rs.getBlob()方法返回的Blob对象造型为oracle的BLOB对象的时候,老是出错,提示java.lang.ClassCastException: weblogic.jdbc.rmi.SerialOracleBlob。

备注:我用的Connection是从weblogic的连接池里得到的,都无异常。

向各位高手讨教解决问题的方法,急救!

...全文
86 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
iis40 2002-09-12
  • 打赏
  • 举报
回复
1、我的文件只有90几k,
2、如果我用工具导入a.doc文件到数据库,再从数据库里导出到b.doc,这样操作是很正常的,所以工具和过程肯定没问题。用程序把a.doc保存到数据库,再导出到b.doc,这样就出错了,所以还是程序的问题。
hystream 2002-09-12
  • 打赏
  • 举报
回复
首先你在程序里用到了byte[]数组,byte[]最大只能初始化大小为20M,看看你的文件是不是在这个范围之内,(一般情况下a.doc不会太大,远远小于20M)。
再检查你用的是什么数据库工具导出的!
一般情况下,如果保存正确了,导出的时候是没有什么问题的,或者是你导出的过程出了什么问题!
iis40 2002-09-12
  • 打赏
  • 举报
回复
谢谢火鸟,文件是可以存储到blob字段了,但是我倒出数据到另外一个文件的时候,发现此文件数据不全,说明文件不是完全存储到了blob字段。
我把a.doc文件保存到blob字段,再倒出到b.doc,发现b.doc的大小比a.doc小,并且打不开,当然b.doc比空白的时候大,说明b.doc里面已经有数据,不过是坏的数据而已。也就是说明,a.doc没有完全保存到数据库。
不知道是为什么,倒出我是用数据库工具做的,肯定没问题。
hystream 2002-09-12
  • 打赏
  • 举报
回复
package examples.jdbc.oracle;
import java.sql.*;
import java.io.*;
import weblogic.jdbc.common.*;
import weblogic.jdbc.common.*;
import java.util.Properties;

public class OracleBlobClob {

public static void main(String argv[])
{
String user = "scott";
String password = "tiger";
String server = "DEMO";

try {
for (int i = 0; i < argv.length; i++)
{
if (argv[i].equals("-user")) {
i++;
user = (argv[i].equals("null") ? "" : argv[i]);
}
else if (argv[i].equals("-password")) {
i++;
password = (argv[i].equals("null") ? "" : argv[i]);
}
else if (argv[i].equals("-server")) {
i++;
server = (argv[i].equals("null") ? "" : argv[i]);
}
}
} catch(ArrayIndexOutOfBoundsException aiobe) {
System.err.println("\nUsage: java examples.jdbc.oracle.OracleBlobClob [options] \n\n" +
"where options include:\n" +
" -user <user> User name to be passed to database.\n" +
" -password <password> User password to be passed to database.\n" +
" -server <server> DNS name of database server.\n");
System.exit(1);
}

java.sql.Blob myBlob = null;
java.sql.Clob myClob = null;
java.sql.Connection conn = null;

// get a connection to the Oracle DBMS
// substitute the name of the machine hosting your
// Oracle server for myOracle8Server

Properties props = new Properties();
props.put("user", user);
props.put("password", password);
props.put("server", server);

try {
Driver myDriver = (Driver)
Class.forName("weblogic.jdbc.oci.Driver").newInstance();
conn = myDriver.connect("jdbc:weblogic:oracle" , props);


// set Oracle's Auto Commit feature to false.
// This is necessary when manipulating Blobs and Clobs.
conn.setAutoCommit(false);

// ============== Create Table ==================
// Create a table with a Blob and Clob column
try {
// if table does not exist, create it.
Statement crstmt = conn.createStatement();
System.out.println("\nCreating table with Blobs and Clobs...");
crstmt.execute("create table lobtest (id int, blobcol Blob, clobcol Clob)");
crstmt.close();
}
catch (Exception e) {
System.out.println("Exception: " + e);
System.out.println("Table already exists. Dropping it and re-creating...");
Statement crstmt2 = conn.createStatement();
crstmt2.execute("drop table lobtest");
crstmt2.execute("create table lobtest (id int, blobcol Blob, clobcol Clob)");
crstmt2.close();
}
System.out.println("Table created.");


// ============== Initializing blob and clob values ==================
Statement stmt = conn.createStatement();
System.out.println("\nInserting row with blank blob and clob columns...");
stmt.execute("insert into lobtest values (44,EMPTY_BLOB(),EMPTY_CLOB())");
System.out.println("Row has been inserted.");

// ============== Manipulating the Blob column ======================
// get a reference to the Blob column
stmt.execute("select * from lobtest where id=44");
ResultSet rs = stmt.getResultSet();
while ( rs.next() ) {
myBlob = rs.getBlob("blobcol");
}

// Create a byte array and store some data in it
System.out.println("\nCreating the following byte array:");
int STREAM_SIZE = 10;
byte[] b = new byte[STREAM_SIZE];
for (int i=0; i < STREAM_SIZE; i++) {
b[i] = (byte)(40 + (i%20)); // range 40-60
System.out.println("byte[" + i + "] = " + b[i]);
}

// Write the byte array to a stream and store it in the Blob column
System.out.println
("\nWriting the byte array to a stream" +
" and storing it in the table as a blob...");
InputStream is = new ByteArrayInputStream(b);
java.io.OutputStream os =
((weblogic.jdbc.common.OracleBlob) myBlob).getBinaryOutputStream();
byte[] inBytes = new byte[STREAM_SIZE];
int numBytes = is.read(inBytes);

// write the input stream to the output stream
while (numBytes > 0) {
os.write(inBytes, 0, numBytes);
numBytes = is.read(inBytes);
}
// The flush() method causes the data to be written to the table
os.flush();

// read back the blob
System.out.println("\nReading the blob back from the table and displaying:");
Statement readblob = conn.createStatement();
readblob.execute("select * from lobtest where id=44");
ResultSet rsreadblob = readblob.getResultSet();

// read the blob into a byte array and display
byte[] r = new byte[STREAM_SIZE];
while ( rsreadblob.next() ) {
Blob myReadBlob = rsreadblob.getBlob("blobcol");
java.io.InputStream readis = myReadBlob.getBinaryStream();
for (int i=0 ; i < STREAM_SIZE ; i++) {
r[i] = (byte) readis.read();
System.out.println("output [" + i + "] = " + r[i]);
}
}


// create some character data to work with
String ss = "abcdefghijklmnopqrstuvwxyz";
System.out.println("\nCreated the following string to be stored as a clob:\n" +
ss);

// ============== Manipulating the Clob column ======================
// get a reference to the clob column
stmt.execute("select * from lobtest where id=44");
ResultSet crs = stmt.getResultSet();
while ( crs.next() ) {
myClob = crs.getClob("clobcol");

java.io.OutputStream osss =
((weblogic.jdbc.common.OracleClob) myClob).getAsciiOutputStream();
byte[] bss = ss.getBytes("ASCII");
osss.write(bss);
osss.flush();
}
conn.commit();

// read back the clob
System.out.println("\nReading the clob back from the table and displaying:");
Statement readclob = conn.createStatement();
readclob.execute("select * from lobtest where id=44");
ResultSet rsreadclob = readclob.getResultSet();

// read the clob in as and ASCII stream, write to a character array, and display
while ( rsreadclob.next() ) {
Clob myReadClob =rsreadclob.getClob("clobcol");
java.io.InputStream readClobis = myReadClob.getAsciiStream();
char[] c = new char[26];
for (int i=0 ; i < 26 ; i++) {
c[i] = (char) readClobis.read();
System.out.println("output [" + i + "] = " + c[i]);
}
}

// Drop the table and clean up connections
System.out.println("\nDropping table...");
Statement dropstmt = conn.createStatement();
dropstmt.execute("drop table lobtest");
System.out.println("Table dropped.");

} catch (Exception e) {
System.out.println("Exception was thrown: " + e.getMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException sqle) {
System.out.println("SQLException was thrown: " + sqle.getMessage());
}
}
}

}
iis40 2002-09-12
  • 打赏
  • 举报
回复
java.lang.ClassCastException: weblogic.jdbc.rmi.SerialOracleBlob
java.lang.ClassCastException: weblogic.jdbc.rmi.SerialResultSet

是什么意思啊,请指教
iis40 2002-09-12
  • 打赏
  • 举报
回复
还是一样,以下是我的原始代码,docname是文件名称字段,doccontent是文件对象BLOB字段,请指教。谢谢。

import java.sql.Blob;
import javax.naming.*;
import javax.sql.DataSource;
import java.sql.*;
import java.io.*;
import java.util.Properties;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.BLOB;

public class DocBlob {

public DocBlob(){

}

public Connection getConnection() {
Context ctx=null;
DataSource ds=null;
Connection cn=null;
try{
Properties p=new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
ctx=new InitialContext(p);
ds=(DataSource)ctx.lookup("OracleDataSource");

return ds.getConnection();
}
catch(Exception e){
System.out.print("sdfdg");
}
return cn;
}

public void setBlob(String docName){
Connection cn=null;
PreparedStatement ps=null;
BufferedInputStream in=null;
OutputStream out=null;


try{
cn=this.getConnection();
cn.setAutoCommit(false);
System.out.print("b");
String sqlstr="insert into test_blob(docname,doccontent) values(?,empty_blob())";
ps=cn.prepareStatement(sqlstr);
ps.setString(1,docName);
ps.executeUpdate();//将文件流插入到数据库中。
ps=cn.prepareStatement("select doccontent from test_blob where docname=? for update");
ps.setString(1,docName);
System.out.print("c");
ResultSet rs =ps.executeQuery();
if(rs.next())
{
System.out.print("d");
BLOB blob = ((OracleResultSet)rs).getBLOB("doccontent");
System.out.print("e");
out=blob.getBinaryOutputStream();
int bufferSize=blob.getBufferSize();
in=new BufferedInputStream(new FileInputStream(docName),bufferSize);
byte[] b=new byte[bufferSize];
int count=in.read(b,0,bufferSize);
while(count!=-1){
out.write(b,0,count);
count=in.read(b,0,bufferSize);
}

cn.commit();
}
}
catch(Exception e){
try{cn.rollback();}
catch(SQLException se){}
e.printStackTrace();
}
finally{
try{
out.close();
out=null;
in.close();
in=null;
ps.close();
cn.close(); }
catch(Exception e){

}
}
}
hystream 2002-09-12
  • 打赏
  • 举报
回复
public void saveBlobIn(String blobname, byte[] txbyte) throws SQLException, IOException {
Blob blob=m_rs.getBlob(blobname);
OutputStream outstream = ((weblogic.jdbc.common.OracleBlob)blob).getBinaryOutputStream();
outstream.write(txbyte,0,txbyte.length);
outstream.close();
}
wjmmml 2002-09-12
  • 打赏
  • 举报
回复
oracle.sql.BLOB blob = ((OracleResultSet)rs).getBLOB("doccontent")

试试
iis40 2002-09-12
  • 打赏
  • 举报
回复
wjmmml(笑着悲伤) ,好兄弟,改成你的代码:
blob = ((OracleResultSet)rs).getBLOB("doccontent")
错误变成下面这样了,真是痛苦啊
java.lang.ClassCastException: weblogic.jdbc.rmi.SerialResultSet
也就是说还是造型的问题。
wjmmml 2002-09-12
  • 打赏
  • 举报
回复
package word;

import java.util.*;
import java.sql.*;
import java.io.*;
import java.text.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;

public class read {

public static void main(String[] args) throws SQLException,IOException
{
Connection conn = null;
PreparedStatement stmt = null;
InputStream in = null;
OutputStream out = null;
BLOB blob = null;
ResultSet rs = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn =DriverManager.getConnection ("jdbc:oracle:thin:@wuhan:1521:oral",
"system", "manager");
conn.setAutoCommit(false);

stmt = conn.prepareStatement("Select w FROM tt WHERE id = ?");
stmt.setInt(1, 2);
rs = stmt.executeQuery();
if(rs.next()) {
blob = ((OracleResultSet)rs).getBLOB("w");
}
in = blob.getBinaryStream();
out = new FileOutputStream("bb.doc");
int bufferSize = blob.getBufferSize();
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
stmt.clearParameters();
buffer = null;
}
catch(SQLException ex) {
ex.printStackTrace();

}
catch(Exception e) {
e.printStackTrace();

}

finally {
try {
in.close();
out.close();
conn.commit();
stmt.close();
conn.close();
in = null;
blob = null;
rs = null;
out = null;
conn = null;
stmt = null;
}
catch(SQLException e) {

}
}

}
}
iis40 2002-09-12
  • 打赏
  • 举报
回复
高手在哪里?高手在哪里?

1,236

社区成员

发帖
与我相关
我的任务
社区描述
企业软件 中间件技术
社区管理员
  • 中间件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧