第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

hibernate中寫(xiě)入寫(xiě)出picture(Blob)的方法

標(biāo)簽:
Java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.*;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

//测试类
public class StudentTest {

private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;

@Before
public void init(){
    //创建配置对象
    Configuration config=new Configuration().configure();
    //创建会话工厂对象
    sessionFactory=config.buildSessionFactory();
    //会话对象
    session=sessionFactory.openSession();
    //开启事务
    transaction =session.beginTransaction();

}

@After
public void destroy(){
    transaction.commit();//提交事务
    session.close();//关闭会话
    sessionFactory.close();//关闭会话工厂
}

//@Test
public void testSaveStudent(){

    //生成学生对象
    student s=new student( 0, "你的傻儿子", "男", new Date(),"守望之海");
    student s1=new student(1,"LG楠","男",new Date(),"守望之海");
    session.save(s);
    session.save(s1);//保存对象进数据库
}

//@Test
public void testWriteBlob() throws Exception
{
    student stu=new student(2,"达尼的双鱼","男",new Date(),"守望之海");
    //先获得照片文件
    File f=new File("E:"+File.separator+"test.jpg");
    //获得照片文件的输入流
    InputStream input=new FileInputStream(f);
    //创建一个Blob对象
    Blob image=Hibernate.getLobCreator(session).createBlob(input, input.available());
    //设置照片属性
    stu.setPicture(image);
    //保存学生
    session.save(stu);
}

@Test
public void testReadBlob() throws Exception
{
    student s=(student) session.get(student.class,2);
    //获得Blob对象
    Blob image=s.getPicture();
    //获得照片的输入流
    InputStream input=image.getBinaryStream();
    //创建输出流
    File file=new File("E:"+File.separator+"copy_test.jpg");
    //获得输出流
    OutputStream output=new FileOutputStream(file);
    //创建缓冲区
    byte[] buff=new byte[input.available()];
    input.read(buff);
    output.write(buff);
    input.close();
    output.close();
}

}

點(diǎn)擊查看更多內(nèi)容
3人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消