The pictures in the database are taken out through the actions of the stream and Jpegcodec, JPEGImageEncoder classes. Note in detail, be aware of the additional driver package that introduces MySQL, to modify the user name and password of the database and the URL. Hope to be of help to everyone. import java.awt.image.bufferedimage;import java.io.*;import java.sql.blob;import java.sql.connection;import java.sql.drivermanager;import java.sql.preparedstatement;import java.sql.resultset;import com.sun.image.codec.jpeg.jpegcodec;import Com.sun.image.codec.jpeg.jpegimageencoder;import javax.imageio.imageio;public class getimage { //Connection Database Information static String user = "username";//Use your own user name static string driver = "Com.mysql.jdbc.Driver"; static string password = " Password ";//Use your own password static String url = " jdbc:mysql://localhost:4017/image ";//Own database address public static void main (String[]args) { try { Class.forName (driver);//Load Driver connection con = drivermAnager.getconnection (Url,user,password);//Get Connection object preparedstatement ps = con.preparestatement ("select photo from photo where name = ' Z '"); resultset rs = ps.executequery (); rs.next ();//move to the first line Blob blob = (BLOB) Rs.getblob ("photo");//Gets the Blob object in the database, which is the image file file = new file ("d:/1.jpg");//Specify the file's output path and filename if (!file.exists ())//Judgment, If the file does not exist, create the file file.createnewfile (); fileoutputstream fos = new fileoutputstream (file);//create the output stream of files according to the path specified bufferedinputstream in = new bufferedinputstream (Blob.getbinarystream ());//writes the resulting file to the stream //system.out.println ( in);//test content is empty bufferedimage image = imageio.read (in);//convert stream to Image object jpegimageencoder encOder = jpegcodec.createjpegencoder (FOS); encoder.encode (image); } catch (exception e) { e.printstacktrace (); } }}
This article is from the "low-key Temperament" blog, please be sure to keep this source http://6641091.blog.51cto.com/6631091/1543300