Hibernate + MySQL image reading

Source: Internet
Author: User

I found some information on the Internet and tried it again!

The general process is to read the image file with inputstream, convert it into a bytecode file, store the bytecode in the database, and store the image with the clob data type in the database. The following code is used:

 

Package image;

Import java. AWT. image. bufferedimage;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. SQL. sqlexception;
Import java. util. date;
Import java. util. List;

Import javax. ImageIO. ImageIO;

Import many2133. hbnutil;
Import many2133. student;

Import org. hibernate. hibernate;
Import org. hibernate. hibernateexception;
Import org. hibernate. query;
Import org. hibernate. Session;
Import org. hibernate. transaction;

Public class test ...{

/***//**
* @ Param ARGs
* @ Throws exception
*/
Public static void main (string [] ARGs) throws exception ...{
// Insert ();
Show ();
}
Public static person show () throws exception ...{
Session S = NULL;
Transaction TRAN = NULL;
String hql = "from person ";
Try ...{
S = hbnutil. getsession ();
TRAN = S. begintransaction ();
Query query = S. createquery (hql );
List list = query. List ();
System. Out. println (list. Size ());
Int I = 0;
For (person :( list <person>) List )...{
Int Len = (INT) person. getbl (). Length ();
Byte [] temp = person. getbl (). getbytes (1, Len );
String dir = "C:/temp _" + person. getname () + ". jpg ";
Fileoutputstream Fos = new fileoutputstream (DIR );
FOS. Write (temp );
}
Tran. Commit ();
} Catch (hibernateexception e )...{
E. printstacktrace ();
Tran. rollback ();
} Finally ...{
Hbnutil. releasesession (s );
}
Return NULL;
}
Public static void insert () throws exception ...{
Session S = NULL;
Transaction TRAN = NULL;

Try ...{
Fileinputstream FCM = new fileinputstream ("C:/temp/112.jpg ");
System. Out. println (FCM. Available ());
Person = new person ();
Person. setname ("Teng ");
Person. setbl (hibernate. createblob (FCM ));

S = hbnutil. getsession ();
TRAN = S. begintransaction ();
S. Save (person );
FCM. Close ();
Tran. Commit ();
} Catch (hibernateexception e )...{
E. printstacktrace ();
Tran. rollback ();
} Finally ...{
Hbnutil. releasesession (s );
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.