22Feb/100
Saving files in BLOB table column of a data base
To insert a file, is it in any format, you need call the method setBinaryStream, implemented by PreparedStatement.
PreparedStatemente.setBinaryStream(int index, Inputstream is, int length);
In sample, we set a table called FILEthat contains BLOB column called BIN.
//Normal connection, as any JDBC connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@<IP>:<PORT>:<SID>","<USER>","<PASSWORD>");
//Reading the file and retrieving an InputStream
File file= new File("<COMPLETE_FILE_PATH>");
FileInputStream fis = new FileInputStream(file);
//Preparing statement
PreparedStatement ps = conn.prepareStatement("INSERT INTO FILE(bin) VALUES(?)");
//Passing InputStream and file length
ps.setBinaryStream(1, fis, (int)file.length());
ps.execute();
ps.close();
conn.close();
I used Oracle 8i to execute this sample. I haven't a MySQL/PostgreSQL/MS SQL Server in my dispose, then you'll responsible for testing in this data bases and send me the results, OK
Thanks! Until next time!