jdbc取blob读出实例

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnectionTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Connection con=null;
		Statement statement=null;
		ResultSet result=null;
		Blob bo=null;
		BufferedInputStream  is=null;
		ByteArrayOutputStream cache =null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:@xxxxx.nam.nsroot.net:1212:xxxxx";
			String username = "ICARD_xxxxx";
			String passward = "xxxxx";
			 con = DriverManager.getConnection(url, username,
					passward);
			statement=con.createStatement();
			 result=statement.executeQuery("select aaa.LOG_DATA_DETAILS from MISLOG aaa where subapp_id like '%epp%'");
			while(result.next()){
				bo = result.getBlob(1);
				is = new BufferedInputStream(bo.getBinaryStream());
				cache = new ByteArrayOutputStream();
				byte[] buf = new byte[512];
				int c = -1;

				while(( c = is.read(buf)) != -1) {
				   cache.write(buf, 0, c);
				}
				System.out.println(new String(cache.toByteArray()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(cache!=null)cache.close();
				if(is!=null)is.close();
				if(result!=null)result.close();
				if(statement!=null)statement.close();
				if(con!=null)con.close();
				bo=null;
				cache=null;
				is=null;
				result=null;
				statement=null;
				con=null;
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

	}

}

你可能感兴趣的:(jdbc)