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

相关文章

1、跑马灯之图片循环滚动

最近,公司做了好几个项目,第一个项目首页跑马灯的效果是我们亲爱滴组长同学写的,后面跑马灯的需求改变了而组长同学又搞别的路去了,于是把我叫了过去,组长很亲切滴对我讲,I am very busy,  Robert同学你来搞一下这个跑马灯效果。我很happy的接过了这个任务,不就几张图片滑啊滑么,我觉得应该very easy! 谁知道原来这竟然是个very large坑,从修改了第一个项目的跑马灯效果

iOS 图片模糊效果

iOS 8.0之后,系统提供了自带的模糊效果: UIImageView *imageview = [[UIImageView alloc] init];imageview.frame = CGRectMake(10, 100, self.view.frame.size.width-20, 400);imageview.image = [UIImage imageNamed:@"test.jpg

dpkg: status database area is locked by another process 解决方法

解决办法:sudo rm -rf /var/lib/dpkg/lock 或者:rm -rf /var/lib/dpkg/lock

SpringBoot 学习四:macOS安装MySQL 以及报错解决

从MySQL官网下载Mysql,在本地安装好之后,用数据库连接工具Sequl Pro去连接数据库,发现报错了,报错信息有两种: Error1:Unable to connect to host 127.0.0.1 because access was denied.Double-check your username and password and ensure that access fro

Centos Mysql定时自动备份

一、备份准备&备份测试 1、备份目录准备 #mysql专用目录mkdir /mysql#mysql备份目录mkdir /mysql/backup#mysql备份脚本mkdir /mysql/backup/scripts#mysql备份文件mkdir /mysql/backup/files#mysql备份日志mkdir /mysql/backup/logs 2、备份脚本准备

Mysqldump 备份mysql数据库

1、mysqldump 安装 yum -y install mysql-client    / apt-get install mysql-client 2、使用mysqldump导出固定条件的数据库 (1)导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql (

docker之mysql连接 Authentication plugin ‘caching_sha2_password‘ cannot be loaded

1  1.docker exec -it mysql01(镜像别名) bash  3 // 进入mysql命令行  4   5  2.mysql -uroot -p 密码       6   7   // 修改加密规则 设置新密码  8   9  3.ALTER USER '账号'@'用户域' IDENTIFIED WITH mysql_native_password BY '新密码';   10

Sql Server 对用户权限的授予:Grant、拒绝Deny、收回Revoke

你是否有遇到这种问题: 1.程序出错:PermissionDineDateAccessExcetion 2.Cause: com.microsoft.sqlserver.jdbc.SQLServerException:拒绝了对象”user”的select.insert权限 1.对表增删改查授权 对用户授权,允许其具有对数据表user的更新和删除的操作权限: GRANT UPDATE,DE

Glide4.7.1图片加载库使用姿势

前言 Glide,一个被google所推荐的图片加载库,作者是bumptech。这个库被广泛运用在google的开源项目中。经过多年的迭代,Glide已经成为了安卓开发者最喜爱的图片加载库之一。新版本的使用方式和以前的3.x.x在使用存在区别,以下是演示最新版本的Glide的使用,Glide新的版本也做了较多的优化和更多功能的实现。 官方地址 :https://github.com/bumpt

MYSQL的流程控制语句

一、准备数据 create database ifTest;use ifTest;create table test(id int primary key auto_increment,typeId int not null comment '产品类型:1-普通商品 2-礼品卡 3-非卖品',productName varchar(50) not null comment '产品名称');