Java+mysql picture data saving and reading specific examples

Source: Internet
Author: User

1. CREATE TABLE: Drop table if exists photo;
CREATE TABLE Photo (
ID INT not NULL auto_increment PRIMARY KEY,
Name VARCHAR (+) COMMENT ' name ', photo blob COMMENT ' photos ')
Engine=innodb
DEFAULT Charset=utf8
Collate=utf8_general_ci;

The data storage format for images in MySQL is blob type; A blob is a container that can store binary files.

2. The tool class for writing picture stream data access:

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;

/**
* Binary stream of images read from local files
*
* @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 database driver--mysql-connector-java-5.1.24-bin.jar

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

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 ();
}
}
}
}
}

}
}

Java+mysql picture data saving and reading specific examples

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.