插入与读取Blob类型数据

摘要:
)";PreparedStatementps=conn.prepareStatement;ps.setObject;ps.setObject;ps.setObject;FileInputStreamis=newFileInputStream;ps.setObject;ps.execute();JDBCUtils.closeResource;}//查询数据表customers中的Blob类型字段@TestpublicvoidtestQuery(){Connectionconn=null;PreparedStatementps=null;InputStreamis=null;FileOutputStreamfos=null;ResultSetrs=null;try{conn=JDBCUtils.getConnection();Stringsql="selectid,name,email,birth,photofromcustomerswhereid=?";ps=conn.prepareStatement;ps.setInt;rs=ps.executeQuery();if{//方式一//intid=rs.getInt;//Stringname=rs.getString;//Stringemail=rs.getString;//Datebirth=rs.getDate;//方式二intid=rs.getInt;Stringname=rs.getString;Stringemail=rs.getString;Datebirth=rs.getDate;Customercust=newCustomer;System.out.println;//将Blob的字段下载下来,以文件的方式保存到本地Blobphoto=rs.getBlob;is=photo.getBinaryStream();fos=newFileOutputStream;byte[]buffer=newbyte[1024];intlen;while((len=is.read(buffer))!

BlobTest

packagecom.aff.PreparedStatement;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.sql.Blob;
importjava.sql.Connection;
importjava.sql.Date;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importorg.junit.Test;
importcom.aff.bean.Customer;
importcom.aff.utils.JDBCUtils;

//向数据表Customers插入Blob类型的字段
public classBlobTest {

    //向数据表customers插入Blob类型的字段
@Test
    public void testInsert() throwsException {
        Connection conn =JDBCUtils.getConnection();
        String sql = " insert into customers(name,email,birth,photo)values(?,?,?,?)";
        PreparedStatement ps =conn.prepareStatement(sql);
        ps.setObject(1, "何苗苗");
        ps.setObject(2, "hemiao@163.com");
        ps.setObject(3, "1996-2-3");
        FileInputStream is = new FileInputStream(new File("1.jpg"));
        ps.setObject(4, is);
        ps.execute();
        JDBCUtils.closeResource(conn, ps);
    }

    
    
    //查询数据表customers中的Blob类型字段
@Test
    public voidtestQuery() {
        Connection conn = null;
        PreparedStatement ps = null;
        InputStream is = null;
        FileOutputStream fos = null;
        ResultSet rs = null;
        try{
            conn =JDBCUtils.getConnection();
            String sql = "select id,name,email,birth,photo from customers where id = ?";
            ps =conn.prepareStatement(sql);
            ps.setInt(1, 20);
            rs =ps.executeQuery();
            if(rs.next()) {
                //方式一
                //int id = rs.getInt(1);
                //String name = rs.getString(2);
                //String email = rs.getString(3);
                //Date birth = rs.getDate(4);
                //方式二
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String email = rs.getString("email");
                Date birth = rs.getDate("birth");

                Customer cust = newCustomer(id, name, email, birth);
                System.out.println(cust);

                //将Blob的字段下载下来,以文件的方式保存到本地
                Blob photo = rs.getBlob("photo");
                is =photo.getBinaryStream();
                fos = new FileOutputStream(new File("mm.jpg"));
                byte[] buffer = new byte[1024];
                intlen;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally{
            
            try{
                if(is != null)
                is.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
            try{
                if(fos !=null)
                fos.close();
            } catch(IOException e) {
                e.printStackTrace();
            }
            JDBCUtils.closeResource(conn, ps, rs);
        }
    }
}

文件位置

插入与读取Blob类型数据第1张

免责声明:文章转载自《插入与读取Blob类型数据》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux目录文件与系统启动(2)/etc系统初始化及设置相关重要文件BGI Error:Graphics not initialized (use 'initgraph')错误C 图形图像处理入门(一)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

关于利用RD client远程电脑,和输入法的一些问题

我在寝室,利用转接头,借助手机app“RDclient”成功完成在键盘上输入文字,并在电脑上输出内容。“RDclient”可以完成手机端远程连接电脑端,只需要知道电脑的ip,用户名和密码就可以。“RDclient”这是微软自家发布的软件,还是很不错的。不过,在刚开始连接的时候,输入法遇到一点问题。后来,在手机上,调出输入法打字的界面,更换成英文输入模式,然后...

Caused by: com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed

春季启动正常启动后,计划任务中的数据库查询报告错误。错误消息如下:1Causedby:org.apache。伊巴提斯。例外情况。PersistenceException:2###错误查询数据库。暂停:org.springframework。jdbc。无法获取JdbcConnection异常:无法获取JDBC连接;3estedexetinisom.alibab...

C#Win32API编程之PostMessage

本文以C#调用Win32API函数PostMessage完成指定表单的后台鼠标和键盘模拟为例,大致解释了C#调用非托管代码和Window的消息处理机制。我们可以将PostMessage用于函数。成功与否在很大程度上取决于我们传达的信息是否真实。消息表明消息是什么。请原谅我先讲故事。我希望先解释一下PostMessage函数。这是一个异步操作,如下图所示:调用...

WritableWorkbook 详细用例 (转)

1WritableWorkbookworkbook=工作簿.createWorkbook(newFile(“d:\test.xls”));1Workbookwb=工作簿.getWorkbook(newFile(“src\test\test.xls”));...

easyExcel自动合并单元格

importcom.alibaba.excel.write.handler.CellWriteHandler;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.util.CellRangeAddress;int[]mergeColumnIndex){this.mergeRowInd...

使用 supervisor 管理进程

Supervisor可以在Linux和Mac OS X上运行。Supervisor功能强大,提供了很多功能,但我们可能只需要使用其中的一小部分。为了方便起见,我们将配置分为两部分:管理程序和应用程序。首先,让我们看看supervisord的配置文件。...