Java + MySql instance for saving and reading image data

Source: Internet
Author: User

1. Create a table:

Copy codeThe Code is as follows: drop table if exists photo;
Create table photo (
Id int not null AUTO_INCREMENT primary key,
Name VARCHAR (100) COMMENT 'name ',
Photo blob COMMENT 'photo'
)
ENGINE = InnoDB
Default charset = utf8
COLLATE = utf8_general_ci;

The data storage format of images in MySql is blob. Blob is a container that can store binary files.

2. Tools for accessing image stream data:

Copy codeThe Code is as follows: import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;

Public class ImageUtil {
Private static File file = null;

/**
* Reading the binary stream of an image from a local file
*
* @ Param infile
* @ Return
*/
Public static FileInputStream getImageByte (String infile ){
FileInputStream imageByte = null;
File = new File (infile );
Try {
ImageByte = new FileInputStream (file );
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
Return imageByte;
}

/**
* Read the image stream as an image.
*
* @ Param inputStream
* @ Param path
*/
Public static void readBlob (InputStream inputStream, String path ){
Try {
FileOutputStream fileOutputStream = new FileOutputStream (path );
Byte [] buffer = new byte [1024];
Int len = 0;
While (len = inputStream. read (buffer ))! =-1 ){
FileOutputStream. write (buffer, 0, len );
}
InputStream. close ();
FileOutputStream. close ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}

}
}

3. Save the local file to the database

Need to add MySql database driver -- mysql-connector-java-5.1.24-bin.jar

Copy codeThe Code is as follows: import java. io. IOException;
Import java. io. InputStream;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. SQLException;

Public class ImageInsert {
Public static void main (String [] args ){
Try {
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
} Catch (InstantiationException e ){
E. printStackTrace ();
} Catch (IllegalAccessException e ){
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
String user = "root ";
String password = "root ";
String url = "jdbc: mysql: // localhost: 3306/test? CharacterEncoding = UTF-8 ";
Connection connection = null;
Try {
Connection = DriverManager. getConnection (url, user, password );
} Catch (SQLException e ){
E. printStackTrace ();
}
PreparedStatement preparedStatement = null;
InputStream inputStream = null;
InputStream = ImageUtil. getImageByte ("D: \ temp \ photo1.png ");
Try {
String SQL = "insert into photo (id, name, photo) values (?,?,?) ";
PreparedStatement = connection. prepareStatement (SQL );
PreparedStatement. setInt (1, 1 );
PreparedStatement. setString (2, "Julie ");
PreparedStatement. setBinaryStream (3, inputStream,
InputStream. available ());
PreparedStatement.exe cute ();
} Catch (SQLException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
If (inputStream! = Null)
InputStream. close ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
If (preparedStatement! = Null)
PreparedStatement. close ();
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Try {
Connection. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
}

}
}

4. Read and generate images from the databaseCopy codeThe Code is as follows: import java. io. IOException;
Import java. io. InputStream;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;

Public class ImageGet {
Public static void main (String [] args ){
Try {
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
} Catch (InstantiationException e ){
E. printStackTrace ();
} Catch (IllegalAccessException e ){
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
String user = "root ";
String password = "root ";
String url = "jdbc: mysql: // localhost: 3306/test? CharacterEncoding = UTF-8 ";
Connection connection = null;
Try {
Connection = DriverManager. getConnection (url, user, password );
} Catch (SQLException e ){
E. printStackTrace ();
}
Statement statement = null;
ResultSet resultSet = null;
InputStream inputStream = null;
Try {
Statement = connection. createStatement ();
String SQL = "select p. photo from photo p where id = 1 ";
ResultSet = statement.exe cuteQuery (SQL );
ResultSet. next ();
InputStream = resultSet. getBinaryStream ("photo ");
ImageUtil. readBlob (inputStream, "D: \ temp \ photo2.png ");
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Try {
If (inputStream! = Null)
InputStream. close ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
Try {
If (resultSet! = Null)
ResultSet. close ();
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
If (statement! = Null)
If (statement! = Null)
Try {
Statement. close ();
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
If (connection! = Null)
Try {
Connection. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
}
}

}
}

5. Over!

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.