给你个例子:
public class TrackSample {
private int recordingid;
private int tracknumber;
private File sampleFile;
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;
}
}
public class TrackSample {
private int recordingid;
private int tracknumber;
private File sampleFile;
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;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货