日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql blob图片_使用mysql的Blob字段存取图片

發布時間:2025/3/21 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql blob图片_使用mysql的Blob字段存取图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

只是做實驗,沒有考慮buffer存取和性能。

建立表:

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 interface

Connection 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 test;

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:3306/test";

ResultSet rs = null;

PreparedStatement psmnt = null;

// declare FileInputStream object to store binary stream of given image.

FileOutputStream fos;

try {

File imageout = new File("D:/bbb.jpg");

fos=new FileOutputStream(imageout);

Class.forName("com.mysql.jdbc.Driver").newInstance();

connection = DriverManager.getConnection(connectionURL, "root",

"root");

psmnt = connection

.prepareStatement("select image from example where name=? ");

psmnt.setString(1, "michael");

rs=psmnt.executeQuery();

rs.next();

Blob image_blob=rs.getBlob("image");

InputStream is=image_blob.getBinaryStream();

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.

connection.close();

psmnt.close();

}

}

}

總結

以上是生活随笔為你收集整理的mysql blob图片_使用mysql的Blob字段存取图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。