请高手指点(JDBC技术)

时间:2008-06-11 19:14:38   来源:论坛整理  作者:  编辑:chinaitzhe
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.sql.Blob;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;





import jdbc.util.CloseResourse;

import jdbc.util.ConnectionFactory;

/**

 * create table images(id number(9) primary key,name varchar2(30) not null,image blob)

 * @author Administrator

 *

 */

public class ImageLibraryServer {

    public void addImage(long id,String imagename,String path){

        Connection con = null;

        PreparedStatement ps = null;

        ResultSet rs = null;

        try{

            con = ConnectionFactory.getConnection();

            con.setAutoCommit(false);

            String sql = "insert into images(id,name,image) values(?,?,empty_blob())";

            ps = con.prepareStatement(sql);

            ps.setLong(1, id);

            ps.setString(2, imagename);

            ps.executeUpdate();

            ps.close();

            

            sql = "select image from images where id=? for update";

            ps = con.prepareStatement(sql);

            ps.setLong(1, id);

            rs = ps.executeQuery();

            while(rs.next()){

                Blob image = (Blob)rs.getBlob("image");//找到要写入的位置

                [u]OutputStream out = image.setBinaryStream(0);//[/u]得到输入流,并指定开始读入的位置

                BufferedOutputStream bos = new BufferedOutputStream(out);

                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));

                int c ;

                while((c=bis.read())!=-1){

                    bos.write(c);

                }

                bis.close();

                bos.close();

            }

            con.commit();

        }catch(Exception e){

            e.printStackTrace();

            try {

                con.rollback();

            } catch (SQLException e1) {

                e1.printStackTrace();

            }

        }finally{

            CloseResourse.close(rs, ps, con);

        }

    }

}




划线的地方报异常,请高手给看看,非常感谢!
Exception in thread "main" java.lang.AbstractMethodError: oracle.sql.BLOB.setBinaryStream(J)Ljava/io/OutputStream;

网友回复:参看:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





  Connection con = MyConnection.getORACLEConnection();

        try ...{

            java.sql.PreparedStatement pstm = con.prepareStatement(

                    "select * from testBinary where id='a1'");

            ResultSet rs = pstm.executeQuery();

            rs.next();

            oracle.sql.BLOB blob = (BLOB) rs.getBlob(2);

            InputStream is = blob.getBinaryStream();

            FileOutputStream fi = new FileOutputStream("f:\aaaa.mp3");

            byte[] buff = new byte[1024];

            int len = is.read(buff);

            while (len != -1) ...{

                fi.write(buff);

                len = is.read(buff);



            }

            fi.close();



        } catch (SQLException ex) ...{

        } catch (FileNotFoundException ex) ...{

          ex.printStackTrace();

        } catch (IOException ex) ...{

            ex.printStackTrace();

        }







完整代码: http://blog.csdn.net/caoyinghui1986/archive/2008/04/05/2252772.aspx


网友回复:学习中

网友回复:楼上正解。
网友回复:一楼正解
网友回复:学习拉
网友回复:OutputStream out = image.getBinaryStream(0);
网友回复:该回复于2008-05-01 05:44:18被版主删除
网友回复:该回复于2008-05-01 06:19:45被版主删除
关键字:高手,指点,JDBC,技术,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面