Oracle 操作圖片 (轉)

worldblog發表於2007-12-12
Oracle 操作圖片 (轉)[@more@]

操作圖片

 

Blobtest.
Created with

package testapp; import java..*; import java.io.*; public class Blobtest { public Blobtest() { } public static void main (String args []) throws SQLException, IOException { //write(); read(); } static void read() throws SQLException, IOException { Manager.registerDriver(new oracle.jc.driver.OracleDriver()); // Connect to the database // You can put a database name after the @ sign in the connection URL. Connection conn = DriverManager.getConnection (":oracle:thin:@ebizser:1521:serverdb", "tt", "tiger"); // It's faster when you don't commit automatically conn.setAutoCommit (false); // Create a Statement Statement stmt = conn.createStatement (); // Do a query to get the row with NAME 'StreamExample' ResultSet rset = stmt.executeQuery (" col2 from lobtest where userid=1"); // Get the first row if (rset.next ()) { // Get the data as a Stream from Oracle to the client InputStream gif_data = rset.getBinaryStream (1); // Open a file to store the gif data FileOutputStream = new FileOutputStream ("example.jpe"); // L, reading from the gif stream and writing to the file int c; while ((c = gif_data.read ()) != -1) os.write (c); // Close the file os.close (); } // Close all the res if (rset != null) rset.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } static void write() throws SQLException, IOException { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // Connect to the database // You can put a database name after the @ sign in the connection URL. Connection conn = //DriverManager.getConnection ("jdbc:oracle:thin:@ebizser:1521:serverdb", "scott", "tiger"); DriverManager.getConnection ("jdbc:oracle:oci8:@ebdb", "scott", "tiger"); // It's faster when you don't commit automatically conn.setAutoCommit (false); // Create a Statement File file = new File("C:Documents and Settingsgzb.ECOMMy DocumentsMy Pictures28-3-16933_jinxishan34s.jpe"); InputStream is = new FileInputStream ("C:Documents and Settingsgzb.ECOMMy DocumentsMy Pictures28-3-16933_jinxishan34s.jpe"); PreparedStatement pstmt = conn.prepareStatement ("insert into lobtest (userid, col2 ) values (?, ?)"); pstmt.setBinaryStream(2, is, (int)file.length ()); pstmt.setInt (1, 2); pstmt.execute (); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } }


Blobtest.java
Created with


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-991962/,如需轉載,請註明出處,否則將追究法律責任。

相關文章