用jsp如何实现oracle中blob字段的存取???

linleming 2003-06-11 02:58:08
想对oracle中的blob字段进行添加、删除、提交???如何实现,请高手指教!
...全文
41 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunrain_gao 2003-06-12
  • 打赏
  • 举报
回复
---------------------------------------
-------------- BLOB ---------------
---------------------------------------
/**ORACLE 中 将 byte[] -> BLOB
---------------------------------------
---------------------------------------
final String SQL_INSERT = "insert into test ( pic,id ) values( empty_blob(), 1) ";
//其中empty_blob(),不能少

final String SQL_UPDATE = "select pic from test where id = 1 for update ";
con.setAutoCommit( false );
pstmt = con.prepareStatement( SQL_INSERT);
pstmt.executeUpdate();
pstmt = con.prepareStatement( SQL_UPDATE );
rs = pstmt.executeQuery();

oracle.sql.BLOB b = null;
while (rs.next())
{
b = (oracle.sql.BLOB) rs.getBlob(1);
}
rs.close();

// Updates blob.
// byte[] content ;

OutputStream os = b.getBinaryOutputStream();
ByteArrayInputStream is = new ByteArrayInputStream( content );
byte[] buffer = new byte[ b.getBufferSize() ];
int bytesRead = 0;
while( ( bytesRead = is.read( buffer ) ) != -1 ) os.write( buffer, 0, bytesRead );
os.close();
is.close();
con.commit();


/**ORACLE 中BLOB 类型的读取 BLOB -> OutputStream
---------------------------------------
---------------------------------------
InputStream in = null;
pstmt = con.prepareStatement(“ select pic from test where id =1 " );
rs = pstmt.executeQuery();

if ( rs.next() ) {
in = rs.getBinaryStream( "pic" );
byte[] b = new byte[1024 * 1024];
int len;
OutputStream outs = _Response.getOutputStream();
// 在servlet中用的response,适用于其它OutputStream
while ( ( len = in.read( b ) ) > 0 ) {
outs.write( b, 0, len );
}
in.close();
outs.flush();
outs.close();
}


// BLOB -> File
---------------------------------------
Blob b = rs.getBlob( Column_NO);
InputStream inputStream = b.getBinaryStream();
long length = b.length();
byte[] buffer = new byte[1024];
int lengthRead = 0;
String fileName = "wwww.gif";
FileOutputStream outputStream = new FileOutputStream( fileName );
while ((lengthRead = inputStream.read( buffer )) != -1) {
outputStream.write( buffer ,0, lengthRead );
}
inputStream.close();
outputStream.close();


// File -> BLOB
---------------------------------------
File file = new File( fileName );
int length = (int) file.length();
FileInputStream fileInputStream = new FileInputStream( file );
preparedStatement.setBinaryStream( 1, fileInputStream, length );
preparedStatement.execute();





---------------------------------------
-------------- CLOB -----------------
-----------------对于long row 也可以----

//String 类型的入库; String -> CLOB
---------------------------------------
String value = new String("…………");
Reader bodyReader = null;
String nullString = "";
if(value == null) value = nullString;
bodyReader = new StringReader(value);
pstmt.setCharacterStream(parameterIndex, bodyReader, value.length());

//String 类型的出库 CLOB -> String
---------------------------------------
Reader bodyReader = null;
String value = null;
//String 类型安标准来说没有长度限制,但是一般jdk中String的最大长度是4G
bodyReader = rs.getCharacterStream(columnIndex);
char [] buf = new char[256];
int len;
StringWriter out = new StringWriter(256);
while ((len = bodyReader.read(buf)) >= 0) {
out.write(buf, 0, len);
}
value = out.toString();
out.close();




neuyqy 2003-06-12
  • 打赏
  • 举报
回复
林子,不会问我啊,blob我很强啊!
linleming 2003-06-12
  • 打赏
  • 举报
回复
谢谢各位,正在研究!解决后立即结帖!!!
linleming 2003-06-12
  • 打赏
  • 举报
回复
谢谢各位!!!祝各位万事如意!!!!! 结帖!!!!!!!!
linleming 2003-06-12
  • 打赏
  • 举报
回复
谢谢各位??
linleming 2003-06-12
  • 打赏
  • 举报
回复
各位高手在说简单一些,只是往blob字段中存取字符串。
zhxx 2003-06-11
  • 打赏
  • 举报
回复
http://search.csdn.net/expert/topic/54/5409/2002/7/15/876147.htm
saisai 2003-06-11
  • 打赏
  • 举报
回复
public void create() {
BufferedInputStream in = null;
OutputStream out = null;

try {

// insert a row into the BLOB table, use the empty_blob()
// construct for the BLOB field. empty_blob() creates the
// BLOB locator for Oracle
statement = getStatement(false);
statement.executeUpdate("insert into tracksamples " +
"(recordingid, tracknumber, sample) " +
"values (1, 1, empty_blob())");

// Retrieve the row that was just inserted
resultset =
statement.executeQuery("select sample from tracksamples " +
"where recordingid=1 and tracknumber=1 " +
"for update");

if (resultset.next()) {

// Get the BLOB locator
Blob blob = resultset.getBlob(1);

// Get the output stream which will be used to send
// data to the table. Use Oracle extension because
// JDBC 2.0 does not support writing data to BLOB
out = ((oracle.sql.BLOB) blob).getBinaryOutputStream();

// Let driver compute buffer size for writing to BLOB
int bufferSize = (int) ((oracle.sql.BLOB) blob).getBufferSize();

// Create a buffered stream to read from the file
in = new BufferedInputStream(new FileInputStream(sampleFile),
bufferSize);

// Create a byte buffer and start reading from the file
byte[] b = new byte[bufferSize];
int count = in.read(b, 0, bufferSize);

// write the bytes using the OutputStream
// loop until all bytes are written to the table
System.out.print("Storing data in database.");
while (count != -1) {
out.write(b, 0, count);
System.out.print(".");
count = in.read(b, 0, bufferSize);
}

System.out.println("Complete");

// Close the Input and Output Streams
// The Output stream MUST be closed before the commit
out.close();
out = null;
in.close();
in = null;

// And finally, commit the changes
connection.commit();
}
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (Exception ignored) {}
} finally {

// if an exception occurred, the streams may not have been closed
// so close them here if needed
if (out != null) {
try {
out.close();
} catch (Exception ignored) {}
}
if (in != null) {
try {
in.close();
} catch (Exception ignored) {}

}
close();
}
return;
}
linleming 2003-06-11
  • 打赏
  • 举报
回复
谁会呀!指点一下!谢谢了!!!!!!

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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