Oracle 操作图片
作者:网络 来源:佚名 更新时间:2008-02-09 10:44:37
点击:0
oracle 操作图片
blobtest.java created with jbuilder
package testapp;import java.sql.*;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 { 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"); // 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 ("select 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 os = new fileoutputstream ("example.jpe"); // loop, 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 resources 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 settings\\gzb.ecom\\my documents\\my pictures\\28-3-16933_jinxishan34s.jpe"); inputstream is = new fileinputstream ("c:\\documents and settings\\gzb.ecom\\my documents\\my pictures\\28-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.javacreated with jbuilder