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 );
}
}
}