import java.sql.*; import java.io.*;// import the Oracle JDBC extension packages import oracle.sql.*; import oracle.jdbc.*;public class LobExample1 { public static void main(String [] args) throws SQLException, IOException { // register the Oracle JDBC drivers DriverManager.registerDriver( new oracle.jdbc.OracleDriver() ); // create a Connection object, and connect to the database // as lob_user using the Oracle JDBC Thin driver Connection myConnection = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:ORCL", "lob_user", "lob_password" ); // disable auto-commit mode myConnection.setAutoCommit(false); // create a statement object Statement myStatement = myConnection.createStatement(); String sourceDirectory = "C:\\sample_files\\"; writeCLOB(myStatement, sourceDirectory + "textContent.txt"); writeBLOB(myStatement, sourceDirectory + "binaryContent.doc"); addBFILE(myStatement, "SAMPLE_FILES_DIR", "textContent.txt"); addBFILE(myStatement, "SAMPLE_FILES_DIR", "binaryContent.doc"); // close the JDBC objects myStatement.close(); myConnection.close(); } // end of main() private static void writeCLOB( Statement myStatement, String fileName ) throws SQLException, IOException { // step 1: initialize the LOB column to set the LOB locator myStatement.executeUpdate( "INSERT INTO clob_content(file_name, clob_column) " + "VALUES ('" + fileName + "', EMPTY_CLOB())" ); // step 2: retrieve the row containing the LOB locator ResultSet clobResultSet = myStatement.executeQuery( "SELECT clob_column " + "FROM clob_content " + "WHERE file_name = '" + fileName + "' " + "FOR UPDATE" ); clobResultSet.next(); // step 3: create a LOB object and read the LOB locator CLOB myClob = ((OracleResultSet) clobResultSet).getCLOB("clob_column"); // step 4: get the chunk size of the LOB from the LOB object int chunkSize = myClob.getChunkSize(); // step 5: create a buffer to hold a block of data from the file char [] textBuffer = new char[chunkSize]; // step 6: create a file object File myFile = new File(fileName); // step 7: create input stream objects to read the file contents FileInputStream myFileInputStream = new FileInputStream(myFile); InputStreamReader myReader = new InputStreamReader(myFileInputStream); BufferedReader myBufferedReader = new BufferedReader(myReader); // step 8: read the file contents and write it to the LOB long position = 1; int charsRead; while ((charsRead = myBufferedReader.read(textBuffer)) != -1) { // write the buffer contents to myClob using the putChars() method myClob.putChars(position, textBuffer); // increment the end position position += charsRead; } // end of while // step 9: perform a commit myStatement.execute("COMMIT"); // step 10: close the objects used to read the file myBufferedReader.close(); myReader.close(); myFileInputStream.close(); System.out.println("Wrote content from file " + fileName + " to CLOB"); } // end of writeCLOB() private static void writeBLOB( Statement myStatement, String fileName ) throws SQLException, IOException { // step 1: initialize the LOB column to set the LOB locator myStatement.executeUpdate( "INSERT INTO blob_content(file_name, blob_column) " + "VALUES ('" + fileName + "', EMPTY_BLOB())" ); // step 2: retrieve the row containing the LOB locator ResultSet blobResultSet = myStatement.executeQuery( "SELECT blob_column " + "FROM blob_content " + "WHERE file_name = '" + fileName + "' " + "FOR UPDATE" ); blobResultSet.next(); // step 3: create a LOB object and read the LOB locator BLOB myBlob = ((OracleResultSet) blobResultSet).getBLOB("blob_column"); // step 4: get the chunk size of the LOB from the LOB object int chunkSize = myBlob.getChunkSize(); // step 5: create a buffer to hold a block of data from the file byte [] byteBuffer = new byte[chunkSize]; // step 6: create a file object to open the file File myFile = new File(fileName); // step 7: create an input stream object to read the file contents FileInputStream myFileInputStream = new FileInputStream(myFile); // step 8: read the file contents and write it to the LOB long position = 1; int bytesRead; while ((bytesRead = myFileInputStream.read(byteBuffer)) != -1) { // write the buffer contents to myBlob using the putBytes() method myBlob.putBytes(position, byteBuffer); // increment the end position position += bytesRead; } // end of while // step 9: perform a COMMIT myStatement.execute("COMMIT"); // step 10: close the objects used to read the file myFileInputStream.close(); System.out.println("Wrote content from file " + fileName + " to BLOB"); } // end of writeBLOB() private static void addBFILE( Statement myStatement, String directory, String fileName ) throws SQLException { myStatement.executeUpdate( "INSERT INTO bfile_content(file_name, bfile_column) " + "VALUES ('" + fileName + "', " + "BFILENAME('" + directory + "', '" + fileName + "'))" ); myStatement.execute("COMMIT"); System.out.println("Added pointer to file " + fileName + " to BFILE in database directory " + directory); } // end of addBFILE()}
/** * 将文件写入数据库 * @param pathname:文件全路径 */ public void writeDBFromFile(String pathname){ myFile=new File(pathname); try{ //将文件转化为文件流 java.io.FileInputStream fis=new FileInputStream(myFile); //得到一个数据库连接 con=new DBconn().getConnection(); pst=con.prepareStatement("update test set mblob=? where id=2"); //pst.setInt(1,2); byte bb[]; String sql="ab"; bb=sql.getBytes(); pst.setBinaryStream(1,new java.io.ByteArrayInputStream(bb),bb.length); //pst.setBinaryStream(1,fis,(int)myFile.length()); pst.executeUpdate(); }catch(Exception e){ System.out.println(e.toString()); }finally{ try{ if (rs!=null) rs.close(); if (pst!=null) pst.close(); if (con!=null) con.close(); }catch(Exception e){ System.out.println(e.toString()); } } }
能给一份存储blob文件的代码吗?
import java.io.*;// import the Oracle JDBC extension packages
import oracle.sql.*;
import oracle.jdbc.*;public class LobExample1 { public static void main(String [] args)
throws SQLException, IOException { // register the Oracle JDBC drivers
DriverManager.registerDriver(
new oracle.jdbc.OracleDriver()
); // create a Connection object, and connect to the database
// as lob_user using the Oracle JDBC Thin driver
Connection myConnection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL",
"lob_user",
"lob_password"
); // disable auto-commit mode
myConnection.setAutoCommit(false); // create a statement object
Statement myStatement = myConnection.createStatement(); String sourceDirectory = "C:\\sample_files\\";
writeCLOB(myStatement, sourceDirectory + "textContent.txt");
writeBLOB(myStatement, sourceDirectory + "binaryContent.doc");
addBFILE(myStatement, "SAMPLE_FILES_DIR", "textContent.txt");
addBFILE(myStatement, "SAMPLE_FILES_DIR", "binaryContent.doc"); // close the JDBC objects
myStatement.close();
myConnection.close(); } // end of main()
private static void writeCLOB(
Statement myStatement,
String fileName
) throws SQLException, IOException { // step 1: initialize the LOB column to set the LOB locator
myStatement.executeUpdate(
"INSERT INTO clob_content(file_name, clob_column) " +
"VALUES ('" + fileName + "', EMPTY_CLOB())"
); // step 2: retrieve the row containing the LOB locator
ResultSet clobResultSet = myStatement.executeQuery(
"SELECT clob_column " +
"FROM clob_content " +
"WHERE file_name = '" + fileName + "' " +
"FOR UPDATE"
);
clobResultSet.next(); // step 3: create a LOB object and read the LOB locator
CLOB myClob =
((OracleResultSet) clobResultSet).getCLOB("clob_column"); // step 4: get the chunk size of the LOB from the LOB object
int chunkSize = myClob.getChunkSize(); // step 5: create a buffer to hold a block of data from the file
char [] textBuffer = new char[chunkSize]; // step 6: create a file object
File myFile = new File(fileName); // step 7: create input stream objects to read the file contents
FileInputStream myFileInputStream = new FileInputStream(myFile);
InputStreamReader myReader =
new InputStreamReader(myFileInputStream);
BufferedReader myBufferedReader = new BufferedReader(myReader); // step 8: read the file contents and write it to the LOB
long position = 1;
int charsRead; while ((charsRead = myBufferedReader.read(textBuffer)) != -1) { // write the buffer contents to myClob using the putChars() method
myClob.putChars(position, textBuffer); // increment the end position
position += charsRead; } // end of while // step 9: perform a commit
myStatement.execute("COMMIT"); // step 10: close the objects used to read the file
myBufferedReader.close();
myReader.close();
myFileInputStream.close(); System.out.println("Wrote content from file " +
fileName + " to CLOB"); } // end of writeCLOB()
private static void writeBLOB(
Statement myStatement,
String fileName
) throws SQLException, IOException { // step 1: initialize the LOB column to set the LOB locator
myStatement.executeUpdate(
"INSERT INTO blob_content(file_name, blob_column) " +
"VALUES ('" + fileName + "', EMPTY_BLOB())"
); // step 2: retrieve the row containing the LOB locator
ResultSet blobResultSet = myStatement.executeQuery(
"SELECT blob_column " +
"FROM blob_content " +
"WHERE file_name = '" + fileName + "' " +
"FOR UPDATE"
);
blobResultSet.next(); // step 3: create a LOB object and read the LOB locator
BLOB myBlob =
((OracleResultSet) blobResultSet).getBLOB("blob_column"); // step 4: get the chunk size of the LOB from the LOB object
int chunkSize = myBlob.getChunkSize(); // step 5: create a buffer to hold a block of data from the file
byte [] byteBuffer = new byte[chunkSize]; // step 6: create a file object to open the file
File myFile = new File(fileName); // step 7: create an input stream object to read the file contents
FileInputStream myFileInputStream = new FileInputStream(myFile); // step 8: read the file contents and write it to the LOB
long position = 1;
int bytesRead; while ((bytesRead = myFileInputStream.read(byteBuffer)) != -1) { // write the buffer contents to myBlob using the putBytes() method
myBlob.putBytes(position, byteBuffer); // increment the end position
position += bytesRead; } // end of while // step 9: perform a COMMIT
myStatement.execute("COMMIT"); // step 10: close the objects used to read the file
myFileInputStream.close(); System.out.println("Wrote content from file " +
fileName + " to BLOB"); } // end of writeBLOB()
private static void addBFILE(
Statement myStatement,
String directory,
String fileName
) throws SQLException { myStatement.executeUpdate(
"INSERT INTO bfile_content(file_name, bfile_column) " +
"VALUES ('" + fileName + "', " +
"BFILENAME('" + directory + "', '" + fileName + "'))"
);
myStatement.execute("COMMIT"); System.out.println("Added pointer to file " +
fileName + " to BFILE in database directory " + directory); } // end of addBFILE()}
* 将文件写入数据库
* @param pathname:文件全路径
*/
public void writeDBFromFile(String pathname){
myFile=new File(pathname);
try{
//将文件转化为文件流
java.io.FileInputStream fis=new FileInputStream(myFile);
//得到一个数据库连接
con=new DBconn().getConnection();
pst=con.prepareStatement("update test set mblob=? where id=2");
//pst.setInt(1,2);
byte bb[];
String sql="ab";
bb=sql.getBytes();
pst.setBinaryStream(1,new java.io.ByteArrayInputStream(bb),bb.length);
//pst.setBinaryStream(1,fis,(int)myFile.length());
pst.executeUpdate();
}catch(Exception e){
System.out.println(e.toString());
}finally{
try{
if (rs!=null) rs.close();
if (pst!=null) pst.close();
if (con!=null) con.close(); }catch(Exception e){
System.out.println(e.toString());
}
}
}
我用weblogic的4类驱动没有问题!