In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to add pictures to mysql. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.
The way to add pictures to mysql: first create a method to read pictures using FileInputStream; then connect to the database and write the sql statement, and execute the sql statement with PreparedStatement.
Related free learning recommendation: mysql video tutorial
How to add a picture to mysql:
1. Effect.
Instead of saving a string, you can look at the data type on the left.
two。 Get blob data
We create a method that uses FileInputStream to read pictures, and ByteArrayOutputStream writes the read data to the byte [] array, and then
Public static byte [] getImgStr (String path) throws IOException {FileInputStream fis = new FileInputStream (path); ByteArrayOutputStream out = new ByteArrayOutputStream (); int len = 0; byte [] b = new byte [1024]; while ((len = fis.read (b))! =-1) {out.write;} / receive out byte [] array = out.toByteArray () Fis.close (); out.close (); return array;}
3. Connect to the database and write sql statements
Create a Blob using Blob, then convert the image data we obtained to blob type, and then execute the sql statement with PreparedStatement because it supports placeholders and there are setBlob methods that can directly write the values in our blob address to the database. And then it's done.
Public static void main (String [] args) {/ * load driver * / try {Class.forName ("com.mysql.cj.jdbc.Driver"); / / get connection String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"; String user= "root"; String password = "123456" Try {Connection connection = DriverManager.getConnection (url,user,password); / * insert picture * / byte [] arr = getImgStr ("picture address"); Blob blob = connection.createBlob (); blob.setBytes (1Maginarr) String sql = "insert into pictures (name,pic,date) values ('Zhang San',?, '2015-01-01')"; PreparedStatement ps = connection.prepareStatement (sql); ps.setBlob (1m blob); ps.executeUpdate ();} catch (SQLException e) {e.printStackTrace () }} catch (ClassNotFoundException | IOException e) {e.printStackTrace ();}} Thank you for reading! On the method of adding pictures to mysql to share here, I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.