如何将图片存入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

相关文章

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据