Jdbc处理oracle中的Blob

来源:互联网 发布:人工智能885728 编辑:程序博客网 时间:2024/04/30 01:23


大文本的读

public class GetBinary {


    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "java", "java");
        PreparedStatement pstmt = conn.prepareStatement("select * from detail");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()){
            File file = new File("C:/"+ rs.getInt(1) +".jpg");
            FileOutputStream fos = new FileOutputStream(file);
            InputStream in = rs.getBinaryStream(2);
            int b = in.read();
            while(b!=-1){
                fos.write(b);
                b = in.read();
            }
            fos.close();
        }
        rs.close();
        pstmt.close();
        conn.close();
        
    }

}


大文本的写

public class SetBinary {
    public static void main(String[] args) throws Exception{
        Class.forName("oracle.jdbc.OracleDriver");
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "java", "java");
        PreparedStatement pstmt = conn.prepareStatement("insert into detail values(?,?)");
        
        File file = new File("C:/Java/zips/Script/picture/03.jpg");
        FileInputStream fis = new FileInputStream(file);
        pstmt.setInt(1, 3);
        //pstmt.setBinaryStream(2, fis);
        pstmt.setBinaryStream(2, fis, file.length());
        int i = pstmt.executeUpdate();
        pstmt.close();
        conn.close();
        fis.close();
    }
}

小文本的读

public class GetBlob {

    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "java", "java");
        PreparedStatement pstmt = conn.prepareStatement("select * from detail where photoid=1");
        
        File file = new File("C:/01.jpg");
        FileOutputStream fos = new FileOutputStream(file);

        ResultSet rs = pstmt.executeQuery();
        if(rs.next()){
            fos.write(rs.getBytes(2));
        }
        rs.close();
        pstmt.close();
        conn.close();
        fos.close();

    }

}


小文本的写

public class SetBlob {

    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "java", "java");
        PreparedStatement pstmt = conn.prepareStatement("insert into detail values(?,?)");
        
        File file = new File("C:/Java/zips/Script/picture/01.jpg");
        byte[] input = new byte[(int)file.length()];
        FileInputStream fis = new FileInputStream(file);
        fis.read(input);
        
        pstmt.setInt(1, 1);
        pstmt.setBytes(2, input);
        
        int i = pstmt.executeUpdate();
        
        pstmt.close();
        conn.close();
        fis.close();
        
    }

}


原创粉丝点击