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;
}
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;
}
-------------- 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();