How does JSP read the mediumblob string in MySQL?

Source: Internet
Author: User

How does JSP read and display the mediumblob string in MySQL?

Stores or extracts image files from the MySQL database.

In some cases, you need to store binary files and slice files in the database. At this time, the data stored in the database is different from the ordinary string storage, we need to process the binary file using Java to process the binary stream API, and then store it. Follow these steps:

When you store files in a database, use standard SQL statements like insert into database (column1, column2 ,..)
Values (V1, V2 ,...); Note that when creating a table for storing binary files, the stored fields should use the Blob type instead of the common varchar type.
Blob is a type of binary file, which has different sizes, such as mediablob and logblob. It stores binary files of different sizes.
Mediablob is enough.

1. See the followingCodeTo store image files in MySQL:

..............................

Private final string insertquery = "insert into employeephoto
(Employee_id, binary_photo, lastmod, created) values (?,?, Now (), now ())";

Public void doinsertstaffpic (string loginname, string source_url ){

Connection conn = NULL;

Preparedstatement pre = NULL;

Try {

// Connect to the database. Here I am using the connection pool configured in struts. Of course, you can also use JDBC to directly connect to the database.

Conn = dbprocess. getconnection ();

// Obtain the image object from the image source and write it to the cache

Image image = new imageicon (source_url). getimage ();

Bufferedimage bimage = new bufferedimage (image. getwidth (null ),

Image. getheight (null), bufferedimage. type_int_rgb );

Graphics BG = bimage. getgraphics ();

BG. drawimage (image, 0, 0, null );

BG. Dispose ();

// Write the image to a binary output stream and store it in byte [] Buf.

Bytearrayoutputstream out = new bytearrayoutputstream ();

ImageIO. Write (bimage, "jpg", out );

Byte [] Buf = out. tobytearray ();

// Obtain the output stream and set it to blob.

Bytearrayinputstream instream = new bytearrayinputstream (BUF );

Pre = conn. preparestatement (insertstaffpicquery );

Pre. setstring (1, loginname );

Pre. setbinarystream (2, instream, instream. Available ());

// Execute write operations such as data

Pre.exe cuteupdate ();

} Catch (exception exc ){

Exc. printstacktrace ();

}

Finally {

Try {

Pre. Close ();

Conn. Close ();

} Catch (sqlexception e ){

E. printstacktrace ();

}

}

}

2. The following code retrieves image files from MySQL and writes them to the local file system:

..............................

Private final string writeoutquery = "insert into employeephoto
(Employee_id, binary_photo, lastmod, created) values (?,?, Now (), now ())";

// Retrive the picture data from database and write it to the local disk

Public void dogetandshowstaffpic (string loginname, string DIR ){

Fileoutputstream output = NULL;

Inputstream input = NULL;

Connection conn = NULL;

Resultset rs = NULL;

Preparedstatement pre = NULL;

Try {

Conn = dbprocess. getconnection ();

Pre = conn. preparestatement (writeoutquery );

Pre. setstring (1, loginname );

Rs = pre.exe cutequery ();

If (Rs. Next ()){

// Obtain binary file data from the database

Blob image = Rs. getblob ("binary_photo ");

// Setup the streams

Input = image. getbinarystream ();

Try {

// Set the write path.

Output = new fileoutputstream (DIR );

} Catch (filenotfoundexception E1 ){

E1.printstacktrace ();

}

// Set read buffer size. Do not set the size too small. If the size is too small, the image may be incomplete.

Byte [] RB = new byte [1024000];

Int CH = 0;

// Process blob

Try {

// Write data to the local file system

While (CH = input. Read (RB ))! =-1 ){

Output. Write (RB, 0, CH );

}

} Catch (ioexception e ){

E. printstacktrace ();

}

Try {

Input. Close ();

} Catch (ioexception e ){

E. printstacktrace ();

}

Try {

Output. Close ();

} Catch (ioexception e ){

E. printstacktrace ();

}

}

} Catch (sqlexception e ){

E. printstacktrace ();

}

Finally {

Try {

Rs. Close ();

Pre. Close ();

Conn. Close ();

} Catch (sqlexception e ){

E. printstacktrace ();

}

}

}

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.