Java connects and stores images with Eclipse and MySQL database--Take out pictures

Source: Internet
Author: User

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

Related Article

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.