通过java向mysql数据库中存取图片

 

 

 学mysql的时候都是做个表格,放的也都是文字内容,虽然我知道长篇的文章和图片或者视频的都是用过文件夹的方式存储的,再讲文件路径存进数据库中。但还是想试试直接往mysql数据库中存取图片。这里我用的是java语言和jdbc实现的

mysql数据库中有一个类型是Blob类型,这是一个二进制类型,通常我们会将图片或音像文件转成二进制再存入数据库中,Blob分为以下几种:

  • TinyBlob 最大 255
  • Blob 最大 65K
  • MediumBlob 最大 16M
  • LongBlob 最大 4G

除了jdbc的连接以外,我们需要用到文件的输入、输出流。实现两个方法,一个方法向数据库中存图像,另一个方法从数据库中读取图像并存在电脑本地

import java.io.*;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;


public class Database {


    //JDBC驱动名
    String JDBC_DRIVER = "com.mysql.jdbc.Driver";

    //数据库URL:这里的tt是数据库名称
    String JDBC_URL = "jdbc:mysql://localhost:3306/daImage?useSSL=false&serverTimezone=UTC";

    //        数据库的用户名与密码
    String USER = "root";
    String PASS = "admin123";

    //通过DriverManager类获得该连接对象才能访问数据库
    Connection connection = null;

    //        通过Connection获得该结果对象用于执行静态的SQL语句
//    Statement statement = null;
    PreparedStatement preparedStatement = null;

    String path;
    FileInputStream fileInputStream;


    
    Database() {


//            注册JDBC驱动
        try {
            Class.forName(JDBC_DRIVER);

//            数据库的连接:通过DriverManager类的getConnection方法,传入三个参数:数据库URL、用户名、用户密码,实例化connection对象
            connection = DriverManager.getConnection(JDBC_URL, USER, PASS);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }



    public void add() {
        //            定义数据库查询语句:查询aa表中的name、sex两列数据
        String sql = "insert into taImage values(?,?,?) ";
//        读取图片,图片放在电脑本地,所以我这里手动复制了路径
        File file = new File("/Users/liuliu/Desktop/vv.jpeg");


        try {
            FileInputStream fi = new FileInputStream(file);
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, 2);
            preparedStatement.setString(2, "图片一");
            preparedStatement.setBlob(3, fi);

//            执行查询语句
            int f = preparedStatement.executeUpdate();

            if (f > 0) {
                System.out.println("插入成功");
            } else {
                System.out.println("插入失败");
            }

            preparedStatement.close();
            connection.close();

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


    }

    public void select() {

        Blob get_image;

        String sql = "select* from taImage";
        try {
//            将读取到的图片存放到指定的路径中
            FileOutputStream fileOutputStream = new FileOutputStream("/Users/liuliu/Desktop/bb.jpg");

            preparedStatement = connection.prepareStatement(sql);

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {

                get_image = resultSet.getBlob("photo");
//                将读取到的Blob对象转成字节流
                inputStream = get_image.getBinaryStream();
                int a;
                byte b[] = new byte[1014];
                while ((a = inputStream.read(b)) != -1) {
                    fileOutputStream.write(b, 0, a);
                }


            }


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}

猜你喜欢

转载自www.cnblogs.com/lyd447113735/p/11813537.html