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!