Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of adding pictures to mysql

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the method of adding pictures to mysql. I hope you will gain a lot after reading this article. Let's discuss it together.

The method of adding a picture to mysql: first, set the field type of the picture stored in the database to the blob binary large object type; then convert the picture stream to binary; finally, insert the picture into the database.

Normal images are stored either on local disk or in the database. It's easy to save it locally, and now I'm here to write down how to save the picture in the mysql database.

If you want to save the picture in the database, you need to convert the picture to binary.

1. The field type of the picture stored in the database should be blob binary large object type.

two。 Convert the picture stream to binary

Here is a code example.

I. Database

CREATE TABLE `photo` (`id` int (11) NOT NULL, `name` varchar (255) DEFAULT NULL, `photo`blob, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8

II. Database links

/ * / package JdbcImgTest;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/** * @ author Administrator * * / public class DBUtil {/ / define database connection parameters public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; public static final String URL = "jdbc:mysql://localhost:3306/test"; public static final String USERNAME = "root" Public static final String PASSWORD = "root"; / / Register the database driver static {try {Class.forName (DRIVER_CLASS_NAME);} catch (ClassNotFoundException e) {System.out.println ("Registration failed!") ; e.printStackTrace ();}} / / get connection public static Connection getConn () throws SQLException {return DriverManager.getConnection (URL, USERNAME, PASSWORD);} / / close connection public static void closeConn (Connection conn) {if (null! = conn) {try {conn.close () } catch (SQLException e) {System.out.println ("failed to close connection!") ; e.printStackTrace ();}} / Test / * public static void main (String [] args) throws SQLException {System.out.println (DBUtil.getConn ());} * /}

3. Picture stream

Package JdbcImgTest;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;/** * @ author Administrator * * / public class ImageUtil {/ / read the local image to get the input stream public static FileInputStream readImage (String path) throws IOException {return new FileInputStream (new File (path)) } / / get the output stream public static void readBin2Image (InputStream in, String targetPath) {File file = new File (targetPath); String path = targetPath.substring (0, targetPath.lastIndexOf ("/")); if (! file.exists ()) {new File (path). Mkdir ();} FileOutputStream fos = null Try {fos = new FileOutputStream (file); int len = 0; byte [] buf = new byte [1024]; while ((len = in.read (buf))! =-1) {fos.write (buf, 0, len);} fos.flush () } catch (Exception e) {e.printStackTrace ();} finally {if (null! = fos) {try {fos.close () } catch (IOException e) {e.printStackTrace ();}

IV. Transcoding storage

Package JdbcImgTest;import java.io.FileInputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/** * @ author Administrator tests writing to and reading from the database * / public class ImageDemo {/ / inserting pictures into the database public static void readImage2DB () {String path = "D:/Eclipse/eclipseWorkspace/TestProject/Img/mogen.jpg" Connection conn = null; PreparedStatement ps = null; FileInputStream in = null; try {in = ImageUtil.readImage (path); conn = DBUtil.getConn (); String sql = "insert into photo (id,name,photo) values"; ps = conn.prepareStatement (sql); ps.setInt (1,1) Ps.setString (2, "Tom"); ps.setBinaryStream (3, in, in.available ()); int count = ps.executeUpdate (); if (count > 0) {System.out.println ("inserted successfully!") ;} else {System.out.println ("insert failed!") ;} catch (Exception e) {e.printStackTrace ();} finally {DBUtil.closeConn (conn); if (null! = ps) {try {ps.close () } catch (SQLException e) {e.printStackTrace ();} / / read public static void readDB2Image () {String targetPath = "C:/Users/Jia/Desktop/mogen.jpg"; Connection conn = null in the database PreparedStatement ps = null; ResultSet rs = null; try {conn = DBUtil.getConn (); String sql = "select * from photo where id =?"; ps = conn.prepareStatement (sql); ps.setInt (1,1); rs = ps.executeQuery () While (rs.next ()) {InputStream in = rs.getBinaryStream ("photo"); ImageUtil.readBin2Image (in, targetPath);}} catch (Exception e) {e.printStackTrace ();} finally {DBUtil.closeConn (conn) If (rs! = null) {try {rs.close ();} catch (SQLException e) {e.printStackTrace () }} if (ps! = null) {try {ps.close ();} catch (SQLException e) {e.printStackTrace () }} / / Test public static void main (String [] args) {/ / readImage2DB (); readDB2Image () }} after reading this article, I believe you have a certain understanding of the method of adding pictures to mysql, want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report