将文件以流的形式保存到oracle数据库中
import java.io.*; import java.sql.*; public class DBTest { public void test() throws Exception { //create table table1 (fld1 varchar2(10), fld_blob long raw); String filename = "getstart.gif"; // file need to be save String sql1 = "insert into table1 (fld1, fld_blob) values(?, ?)"; String sql2 = "select fld_blob from table1 where fld1 = ? "; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = getConnection(); ps = conn.prepareStatement(sql1); InputStream is = new FileInputStream(filename); int len = is.available(); ps.setString(1, "2"); ps.setBinaryStream(2, is, len); ps.executeUpdate(); // read it to check if really save it to database. ps = conn.prepareStatement(sql2); ps.setString(1, "2"); rs = ps.executeQuery(); rs.next(); is = rs.getBinaryStream("fld_blob"); // now write it to a new file. OutputStream os = new FileOutputStream("new_" + filename); byte[] b = new byte[512]; while (is.read(b, 0, b.length) != -1) { os.write(b); } os.close(); } catch (Exception e) { throw e; } finally { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } } private Connection getConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@127.0.0.1:1521:your_sid"; String user = "username"; String password = "password"; Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); return conn; } public static void main(String[] args) throws Exception { new DBTest().test(); } }