spring hibernate mysql blob_spring 和hibernate怎样操作blob字段

//测试用HashMap

HashMap staff=new HashMap();

staff.put("001","true");

staff.put("002","false");

staff.put("003","false");

import oracle.sql.*;//oracle提供的JDBC包

//写入oracle数据库

Connection con=dbConn.getConnection();

con.setAutoCommit(false);

Statement st=con.createStatement();

//关键!!之所以用了这么长时间就是因为没有对BLOB字段初始化!

//插入新的数据时一定要使用EMPTY_BLOB()对BLOB字段初始化!!!

st.executeUpdate("UPDATE TABLE_NAME SET BLOB_VALUE=EMPTY_BLOB() WHERE ID=10");

ResultSet rs = st.executeQuery(

"SELECT BLOB_VALUE " +

"FROM TABLE_NAME " +

"WHERE id = 10 FOR UPDATE"

);

rs.next();

BLOB myBlob =(BLOB) rs.getObject("BLOB_VALUE");

ObjectOutputStream oos=new ObjectOutputStream(myBlob.getBinaryOutputStream());

oos.writeObject(staff);

oos.flush();

st.execute("commit");

……

//从oracle数据库中读出

Connection con=dbConn.getConnection();

Statement st=con.createStatement();

ResultSet rs2 = st.executeQuery(

"SELECT BLOB_VALUE " +

"FROM TABLE_NAME " +

"WHERE id = 10"

);

rs2.next();

BLOB blob =(BLOB) rs.getObject("BLOB_VALUE");

ObjectInputStream ois=new ObjectInputStream(blob.getBinaryStream());

HashMap h1=(HashMap) ois.readObject();

String s1=(String) h1.get("001");

system.out.println(s1);//打印出“true”

ois.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值