oracleblob:Oracle中BLOB读取

import com.unimas.common.exception.X;
import com.unimas.common.i18n.Msg;

import java.sql.*;
import java.io.InputStream;

/**
 * Created by IntelliJ IDEA.
 * User: zhouy
 * To change this template use File | Settings | File Templates.
 */
public class SavaBlob {

    private static final String STRING_DRIVERCLASS = "oracle.jdbc.driver.oracledriver";
    private static final String STRING_CONURL = "jdbc:oracle:thin:@192.168.1.132:1521:t1";

    public SavaBlob() {

    }

    public Connection getConnection() throws X {
        Connection connection = null;
        try {
            Class.forName(STRING_DRIVERCLASS);
            connection = DriverManager.getConnection(STRING_CONURL, "zhouy", "zhouy");
        } catch(Exception e) {
            throw new X().set(new Msg(e.getMessage()));
        }
        return connection;
    }

    public void saveData() throws X {
        Connection  connection = getConnection();
        try {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("insert into t5 values ()") ;
            if(resultSet.next()) {
                Blob blob = resultSet.getBlob(1);
                InputStream inputStream = blob.getBinaryStream();
                long len = blob.length();
                int size = (int)len;
                byte[] data = new byte[size];
                inputStream.read(data);
                inputStream.close();

            }
        } catch (Exception e) {
            throw new X().set(new Msg(e.getMessage()));
        }

    }

    public void getData() throws X {
        Connection  connection = getConnection();
        try {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from t5") ;
            if(resultSet.next()) {
                Blob blob = resultSet.getBlob(1);
                InputStream inputStream = blob.getBinaryStream();
                long len = blob.length();
                int size = (int)len;
                byte[] data = new byte[size];
                inputStream.read(data);
                inputStream.close();

            }
        } catch (Exception e) {
            throw new X().set(new Msg(e.getMessage()));
        }
    }

}

Tags:  oracleinsertblob oracleblobclob oracleselectblob oracleblob

延伸阅读

最新评论

发表评论