First, let's look at the database. Create a New BlobTable table in the database. The table has two fields, one id (primary key) and one picture field, which is a Blob field. Then, Hibernate is used to write and read data to the database. In the POJO class, the picture attribute uses Blob data. The following is the source code packagedao; importjava. io. File; im
First, let's look at the database. Create a New BlobTable table in the database. The table has two fields, one id (primary key) and one picture field, which is a Blob field. Then, Hibernate is used to write and read data to the database. In the POJO class, the picture attribute uses Blob data. The following is the source code package dao; import java. io. File; im
First, let's look at the database. Create a New BlobTable table in the database. The table has two fields, one id (primary key) and one picture field, which is a Blob field. Then, use Hibernate to write and read data to the database.
In the POJO class, the picture attribute uses Blob data.
See the operation source code below
Package dao; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. math. bigDecimal; import java. SQL. blob; import org. hibernate. lobHelper; import org. hibernate. query; import org. hibernate. session; import org. hibernate. transaction; import Factory. hibernateSessionFactory; import entity. bl Obtable; public class BlobDao {private Session session = null; private Transaction tran = null; public BlobDao () {this. session = HibernateSessionFactory. getSession ();} public void saveBlob (Blobtable bigdate, String path) throws IOException {/* InputStream in = this. getClass (). getResourceAsStream (path); byte [] bytes = new byte [in. available ()]; in. read (bytes); in. close (); */File file = new File (path); FileInputS Tream Sox = new FileInputStream (file); byte [] bytes = new byte [FCM. available ()]; FCM. read (bytes); LobHelper lh = session. getLobHelper (); bigdate. setPicture (lh. createBlob (bytes); tran = session. beginTransaction (); try {session. save (bigdate); tran. commit (); System. out. println ("inserted successfully! ");} Catch (Exception e) {System. out. println (" insertion failed! "); Tran. rollback ();} finally {HibernateSessionFactory. closeSession (); FCM. close () ;}} public void getBlob (BigDecimal id, String targetpath) throws Exception {String hql = "From Blobtable where id =? "; Query query = session. createQuery (hql); query. setBigDecimal (0, id); Blobtable bt = (Blobtable) query. uniqueResult (); Blob image = bt. getPicture (); InputStream in = image. getBinaryStream (); OutputStream OS = new FileOutputStream (targetpath); int n =-1; while (n = in. read ())! =-1) {OS. write (n) ;}in. close (); OS. close ();}}
Package Test; import java. io. IOException; import java. math. bigDecimal; import dao. blobDao; import entity. blobtable; public class Test {public static void main (String [] args) {BlobDao bb = new BlobDao (); Blobtable bt = new Blobtable (); bt. setId (new BigDecimal (5); try {String path = "f: \ a.jpg"; bb. saveBlob (bt, path);} catch (IOException e) {e. printStackTrace ();} BlobDao bd = new BlobDao (); try {bd. getBlob (new BigDecimal (1), "e: \ a.jpg"); System. out. println ("written successfully! ");} Catch (Exception e) {e. printStackTrace ();}}}