import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.sql.Clob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import oracle.sql.BLOB; import oracle.sql.CLOB; public class SQLOperater { public static void saveFile(String path) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; String sql = "insert into blob_table values('1','name',empty_blob())"; String sql2 = "select * from blob_table where id=? for update"; String sql3 = "update blob_table set file1=? where id=1"; File file = null; InputStream fis = null; OutputStream os = null; BLOB blob = null; try { file = new File(path); fis = new FileInputStream(file); System.out.println(file.length()); conn = Util.getConn(); conn.setAutoCommit(false); ps = conn.prepareStatement(sql); ps.executeUpdate(); ps.close(); ps = conn.prepareStatement(sql2); ps.setInt(1, 1); rs = ps.executeQuery(); if (rs.next()) { blob = (BLOB) rs.getBlob("file1"); } os = blob.getBinaryOutputStream(); int count = -1, total = 0; System.out.println(blob.getBufferSize()); byte[] data = new byte[blob.getBufferSize()]; while ((count = fis.read(data)) != -1) { total += count; os.write(data, 0, count); os.flush(); } fis.close(); os.close(); ps = conn.prepareStatement(sql3); ps.setBlob(1, blob); ps.executeUpdate(); os.close(); System.out.println("保存成功!"); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { Util.close(null, ps, conn); } } public static void saveFileByClob(String path) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; String sql = "insert into clob_table values('1','name',empty_clob())"; String sql2 = "select * from clob_table where id=? for update"; String sql3 = "update clob_table set file1=? where id=1"; File file = null; InputStreamReader isr = null; BufferedReader br = null; InputStream fis = null; Writer writer = null; CLOB clob = null; try { file = new File(path); fis = new FileInputStream(file); isr = new InputStreamReader(fis, "utf-8"); br = new BufferedReader(isr); System.out.println(file.length()); conn = Util.getConn(); conn.setAutoCommit(false); ps = conn.prepareStatement(sql); ps.executeUpdate(); ps.close(); ps = conn.prepareStatement(sql2); ps.setInt(1, 1); rs = ps.executeQuery(); if (rs.next()) { clob = (CLOB) rs.getClob("file1"); } writer = clob.getCharacterOutputStream(); String s = ""; while (true) { String temp = br.readLine(); if (temp == null) break; s += temp + "/n"; } br.close(); System.out.println("文件内容:" + s); writer.write(s); writer.flush(); ps = conn.prepareStatement(sql3); ps.setClob(1, clob); ps.executeUpdate(); writer.close(); System.out.println("保存成功!"); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { Util.close(null, ps, conn); } } public static void fetchFile(String path) { String sql = "select * from blob_table "; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; OutputStream fos = null; InputStream is = null; try { conn = Util.getConn(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); rs.next(); is = rs.getBinaryStream("file1"); fos = new FileOutputStream(new File(path)); byte[] b = new byte[1024]; int len = 0; while ((len = is.read(b)) != -1) { fos.write(b, 0, len); fos.flush(); } fos.close(); is.close(); System.out.println("查询成功!"); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { Util.close(null, ps, conn); } } public static void fetchFileByClob(String path) { String sql = "select * from clob_table "; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; OutputStreamWriter isw = null; Reader rd = null; try { conn = Util.getConn(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); rs.next(); Clob clob = rs.getClob("file1"); String strNR = clob.getSubString(1, (int) clob.length()); isw = new OutputStreamWriter(new FileOutputStream(new File(path)), "UTF-8"); BufferedWriter bw = new BufferedWriter(isw); bw.write(strNR, 0, strNR.length()); bw.close(); System.out.println("查询成功!"); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { Util.close(null, ps, conn); } } public static void main(String[] args) throws SQLException { String path = "E://READMEee.txt"; // saveFile(path); // fetchFile(path); // saveFileByClob(path); fetchFileByClob(path); } }