Chinaunix首页 | 论坛 | 博客
  • 博客访问: 544388
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-07-07 14:22:50


点击(此处)折叠或打开

  1. package com.lw.database;

  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;

  11. /**
  12.  * CREATE: CREATE TABLE IDCard ( id char(18),pic BLOB);
  13.  * @author fhadmin
  14.  * from fhadmin.cn
  15.  */
  16. public class LOBTest {

  17.     protected static final String DEFAULT_URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";
  18.     protected static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
  19.     
  20.     private Connection connection = null;
  21.     
  22.     public LOBTest() throws ClassNotFoundException, SQLException {
  23.         Class.forName(DRIVER_NAME);
  24.         connection = DriverManager.getConnection(DEFAULT_URL, "user", "password");
  25.     }
  26.     
  27.     public void insert(String id,String path) throws SQLException, IOException {
  28.         PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO IDCard VALUES (?,?)");
  29.         preparedStatement.setString(1, id);
  30.         FileInputStream fileInputStream = new FileInputStream(path);
  31.         preparedStatement.setBlob(2, fileInputStream,fileInputStream.available());
  32.         preparedStatement.execute();
  33.     }
  34.     
  35.     public void get(String id) throws SQLException, IOException {
  36.         PreparedStatement preparedStatement = connection.prepareStatement("SELECT pic FROM IDCard WHERE id = ?");
  37.         preparedStatement.setString(1, id);
  38.         ResultSet results = preparedStatement.executeQuery();
  39.         while(results.next()) {
  40.             FileOutputStream outputStream = new FileOutputStream("/Users/liuwei/temp.png");
  41.             InputStream inputStream = results.getBinaryStream(1);
  42.             int num = -1;
  43.             while((num=inputStream.read())!=-1) {
  44.                 outputStream.write(num);
  45.             }
  46.             outputStream.flush();
  47.             inputStream.close();
  48.             outputStream.close();
  49.         }
  50.     }
  51.     
  52.     public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
  53.         LOBTest test = new LOBTest();
  54.         test.insert("78907656784323", "/Users/liuwei/Documents/bt_next_nor.png");
  55.         test.get("78907656784323");
  56.     }
  57. }

注意:


  MySQL的四种BLOB类型 

    类型  大小(单位:字节) 

      TinyBlob  最大 255B

        Blob  最大 65K 

        MediumBlob  最大 16M 

      LongBlob  最大 4G 

插入图像的时候,注意下图像大小,图像超过该类型所能容纳的最大字节的时候,会报错

阅读(1368) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~