如何将图片存入MySQL中的blob去

2024-05-07 17:38

本文主要是介绍如何将图片存入MySQL中的blob去,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

建立表:

CREATE TABLE example (name VARCHAR(100),city VARCHAR(100),image BLOB,Phone VARCHAR(100));


使用一张图片qqq.jpg

存图片的代码:

package test;import java.sql.*;
import java.io.*;public class SaveImageToDatabase {public static void main(String[] args) throws SQLException {// declare a connection by using Connection interfaceConnection connection = null;/** Create string of connection url within specified format with machine* name, port number and database name. Here machine name id localhost* and database name is mahendra.*/String connectionURL = "jdbc:mysql://localhost:3306/test";/** declare a resultSet that works as a table resulted by execute a* specified sql query.*/ResultSet rs = null;// Declare prepare statement.PreparedStatement psmnt = null;// declare FileInputStream object to store binary stream of given image.FileInputStream fis;try {// Load JDBC driver "com.mysql.jdbc.Driver"Class.forName("com.mysql.jdbc.Driver").newInstance();/** Create a connection by using getConnection() method that takes* parameters of string type connection url, user name and password* to connect to database.*/connection = DriverManager.getConnection(connectionURL, "root","root");// create a file object for image by specifying full path of image// as parameter.File image = new File("D:/qqq.jpg");/** prepareStatement() is used for create statement object that is* used for sending sql statements to the specified database.*/psmnt = connection.prepareStatement("insert into example(name, city, image, Phone) "+ "values(?,?,?,?)");psmnt.setString(1, "michael");psmnt.setString(2, "Delhi");psmnt.setString(4, "123456");fis = new FileInputStream(image);psmnt.setBinaryStream(3, (InputStream) fis, (int) (image.length()));/** executeUpdate() method execute specified sql query. Here this* query insert data and image from specified address.*/int s = psmnt.executeUpdate();if (s > 0) {System.out.println("Uploaded successfully !");} else {System.out.println("unsucessfull to upload image.");}}// catch if found any exception during rum time.catch (Exception ex) {System.out.println("Found some error : " + ex);} finally {// close all the connections.connection.close();psmnt.close();}}
}



将图片从数据库中取出来,放到文件中:

package com.liang.java;import java.sql.*;
import java.io.*;public class GetImageFromDatabase {public static void main(String[] args) throws SQLException {Connection connection = null;String connectionURL = "jdbc:mysql://localhost:3308/thzdatabase";ResultSet rs = null;PreparedStatement psmnt = null;// declare FileInputStream object to store binary stream of given image.FileOutputStream fos;try {File imageout = new File("D:/1111.png");fos=new FileOutputStream(imageout);Class.forName("com.mysql.jdbc.Driver").newInstance();connection = DriverManager.getConnection(connectionURL, "root","111111");psmnt = connection.prepareStatement("select image from test where name=? ");psmnt.setString(1, "michael");rs=psmnt.executeQuery();rs.next();Blob image_blob=rs.getBlob("image");InputStream is=image_blob.getBinaryStream();byte[] b = null;byte [] a = new byte[is.read(b, 0, b.length)];System.out.println(is.toString());int ch = 0;  try {  while((ch=is.read()) != -1){  fos.write(ch);  }  } catch (IOException e1) {  e1.printStackTrace();  } finally{  fos.close();  is.close();  }}// catch if found any exception during rum time.catch (Exception ex) {System.out.println("Found some error : " + ex);} finally {// close all the connections.System.out.println("Found success!");connection.close();psmnt.close();}}
}


这篇关于如何将图片存入MySQL中的blob去的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/967946

相关文章

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

mysql中的服务器架构详解

《mysql中的服务器架构详解》:本文主要介绍mysql中的服务器架构,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、mysql服务器架构解释3、总结1、背景简单理解一下mysqphpl的服务器架构。2、mysjsql服务器架构解释mysql的架

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

MySQL中的InnoDB单表访问过程

《MySQL中的InnoDB单表访问过程》:本文主要介绍MySQL中的InnoDB单表访问过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、访问类型【1】const【2】ref【3】ref_or_null【4】range【5】index【6】

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分