A concrete example of java+mysql image data saving and reading _java

Source: Internet
Author: User

1. Create a table:

Copy Code code as follows:

drop table if exists photo;
CREATE TABLE Photo (
ID INT not NULL auto_increment PRIMARY KEY,
Name VARCHAR (m) COMMENT ' names ',
Photo blob COMMENT ' photo '
)
Engine=innodb
DEFAULT Charset=utf8
Collate=utf8_general_ci;

The data stored in the MySQL format is a blob type; A blob is a container in which binary files can be stored.

2. Write the image stream data Access Tool class:

Copy Code code 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;

/**
* Read binary stream of image from 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 picture stream as a picture
*
* @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 to the database driver--mysql-connector-java-5.1.24-bin.jar

Copy Code code 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.execute ();
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 pictures from the database
Copy Code code 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.executequery (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!

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.