>>分享Java编程技术,对《Java面向对象编程》等书籍提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 29093 个阅读者 刷新本主题
 * 贴子主题:  通过Java编程向数据库存取图片 回复文章 点赞(0)  收藏  
作者:Jacky    发表时间:2018-03-22 22:10:06     消息  查看  搜索  好友  邮件  复制  引用

我们要做的是将一张图片存入Mysql中,在Mysql中用Blob 来存储图片和音频等大的数据项.Blob 按其容量可分为四种,分别为:tinyblob,blob ,mediumblob, longblob.他们的大小分别为:256B,64KB,16MB,4GB.除了容量不同外,这四种的用法一个样.我们这次将用Blob.

首先来创建我们的数据库,

create database  Image;
use Image;

create table country (

id int primary key auto_increment,

name varchar(30),

flag blob,

description varchar(255)

);

数据库创建完毕,下面是Java 的程序代码,它实现的功能是从一个目录中读入一张图片(china.gif) ,然后在把它输出在另一个目录中(1.gif).

*****************************************************************************************************

import java.sql.*;
import java.io.*;

public class TestImage {
private static final String URL = "jdbc:mysql://localhost/Image?user=root&password=root&useUnicode=true";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;
private InputStream inputImage = null;
private OutputStream outputImage = null;

public void blobInsert(String infile) {
  try {
   Class.forName("com.mysql.jdbc.Driver");
   System.out.println("Driver loaded!");

   conn = DriverManager.getConnection(URL);
   System.out.println("Database connected!");

   pstmt = conn
     .prepareStatement("insert into Country (name,flag,description) values (?,?,?)");
   pstmt.setString(1, "china");

   file = new File(infile);
   try {
    inputImage = new FileInputStream(file);
   } catch (FileNotFoundException e) {

    e.printStackTrace();
   }
   pstmt.setBinaryStream(2, inputImage, (int) (file.length()));

   pstmt.setString(3, "A flag of China");
   pstmt.executeUpdate();
   System.out.println("commit successfully");
  } catch (ClassNotFoundException e) {

   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    inputImage.close();
    pstmt.close();
    conn.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }

}

public void readBolb(String path, int id) {
  InputStream is = null;
  byte[] buffer = new byte[4096];

  try {
   Class.forName("com.mysql.jdbc.Driver");
   System.out.println("Driver loaded!");

   conn = DriverManager.getConnection(URL);
   System.out.println("Database connected!");

   pstmt = conn
     .prepareStatement("select flag from Country where id =?");
   System.out.println("select ok");

   pstmt.setInt(1, id);
   rs = pstmt.executeQuery();

   rs.next();
   file = new File(path);
   if (!file.exists()) {
    try {
     file.createNewFile();
    } catch (IOException e) {

     e.printStackTrace();
    }

   }
   try {
    outputImage = new FileOutputStream(file);
    System.out.println(outputImage.toString());
   } catch (FileNotFoundException e) {

    e.printStackTrace();
   }

   Blob blob = rs.getBlob("flag");
   is = blob.getBinaryStream();
   try {
    System.out.println(is.available());
   } catch (IOException e2) {
    
    e2.printStackTrace();
   }
   try {
    System.out.println(is.available());
   } catch (IOException e1) {

    e1.printStackTrace();
   }

   int size = 0;
   try {
    while ((size = is.read(buffer)) != -1) {
     System.out.println(size);
     outputImage.write(buffer, 0, size);

    }
   } catch (IOException e) {

    e.printStackTrace();
   }

  } catch (ClassNotFoundException e) {

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

   e.printStackTrace();
  } finally {
   try {
    is.close();
    outputImage.close();
    pstmt.close();
    conn.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }

}

public static void main(String[] args) {

  new TestImage().blobInsert("c://TDdownload//china.gif");
  new TestImage().readBolb("c://1.gif", 1);
}

}

程序执行完你将在相应的目录下看到一张输出的图片.同时在Mysql 客户端使用:select * from country;将显示一堆的乱麻,这就正常.说明图片已经存入Mysql 中.到次本次试验完毕!


程序猿的技术大观园:www.javathinker.net
  Java面向对象编程-->Swing组件(上)
  JavaWeb开发-->Web运作原理(Ⅲ)
  JSP与Hibernate开发-->映射一对多关联关系
  Java网络编程-->用Spring整合CXF发布Web服务
  精通Spring-->Vue Router路由管理器
  Vue3开发-->组合(Composition)API
  10道Java编程基础练习题
  Java设计模式:接口隔离原则和迪米特法则详解
  Java Optional 解决空指针异常总结
  购书咨询
  内部类的种类和用法
  如何优雅地打印一个Java对象?
  JNI_Java Native Interface的用法
  使用javaNIO实现C/S模式的通信
  Java设计模式:备忘录模式
  java 支持分词的高性能拼音转换工具,速度是 pinyin4j 的两倍
  java比c++强大之处JVM垃圾收集算法
  Java 入门实用代码:从 List列表中 截取子列表
  Java入门实用代码:使用 Enumeration 遍历 HashTable
  Java入门实用代码:向文件写入字符串
  Java线程实现龟兔赛跑
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。