hibernate oracle boolean 数据类型,用hibernate向oracle读取blob数据类型为并下载到本地...
向Oracle讀取16進制byte[]數據
這種方法只能處理總大小小于6M的數據(在默認的java虛擬機設置下)。
①持久化類:
public class Mail implements Serializable {
/**
* 主鍵
*/
private int id;
/**
* 即將傳入數據庫中的文件
*/
private byte[] filedata;
/*此處省略get、set*/
}
②持久化類與hibernate的映射文件:持久化類.hbm.xml
③HibernateUtil.java文件(這個不用去理解復制下來就可以)
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
④用hibernate來讀取并下載到本地:
public class TheMain {
/**
* @param args
*/
public static void main(String[] args) {
//例行公事的兩句,不要忘了任何hibernate操作都要用到的
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tran = session.beginTransaction();
File forDownFile = new File("D:/被下載的數據.jpg");
//從數據庫請求數據
Mail mail = (Mail) session.load(Mail.class, 4);
//如果不要求下載那么寫到這里已經足夠
FileOutputStream fos = null;
try {
//載入文件路徑
fos = new FileOutputStream(forDownFile);
//寫入文件數據
fos.write(mail.getFiledata());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//一定不要忘了釋放資源
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
總結
以上是生活随笔為你收集整理的hibernate oracle boolean 数据类型,用hibernate向oracle读取blob数据类型为并下载到本地...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle10_client的安装,O
- 下一篇: oracle bom展开 sql,Ora