当前位置: 移动技术网 > IT编程>开发语言>Java > javaweb学习总结——使用JDBC处理MySQL大数据

javaweb学习总结——使用JDBC处理MySQL大数据

2019年07月22日  | 移动技术网IT编程  | 我要评论

嗜血撒旦惹火妻,耐特菲姆,全力以赴造句

blob (binary large object),二进制大对象,是一个可以存储二进制文件的容器。在计算机中,blob常常是数据库中用来存储二进制文件的字段类型,blob是一个大文件,典型的blob是一张图片或一个声音文件,由于它们的尺寸,必须使用特殊的方式来处理(例如:上传、下载或者存放到一个数据库)。

一、基本概念

在实际开发中,有时是需要用程序把大文本或二进制数据直接保存到数据库中进行储存的。

对mysql而言只有blob,而没有clob,mysql存储大文本采用的是text,text和blob分别又分为:

tinytext、text、mediumtext和longtext

tinyblob、blob、mediumblob和longblob

二、搭建测试环境

2.1、搭建的测试项目架构

如图:

2.2、编写db.properties配置文件

driver=com.mysql.jdbc.driver
url=jdbc:mysql://localhost:3306/jdbcstudy
username=root
password=xdp

2.3、编写jdbcutils工具类

package me.gacl.utils;

import java.io.inputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.properties;

public class jdbcutils {

  private static string driver = null;
  private static string url = null;
  private static string username = null;
  private static string password = null;
  
  static{
    try{
      //读取db.properties文件中的数据库连接信息
      inputstream in = jdbcutils.class.getclassloader().getresourceasstream("db.properties");
      properties prop = new properties();
      prop.load(in);
      
      //获取数据库连接驱动
      driver = prop.getproperty("driver");
      //获取数据库连接url地址
      url = prop.getproperty("url");
      //获取数据库连接用户名
      username = prop.getproperty("username");
      //获取数据库连接密码
      password = prop.getproperty("password");
      
      //加载数据库驱动
      class.forname(driver);
      
    }catch (exception e) {
      throw new exceptionininitializererror(e);
    }
  }
  
  /**
  * @method: getconnection
  * @description: 获取数据库连接对象
  * @anthor:孤傲苍狼
  *
  * @return connection数据库连接对象
  * @throws sqlexception
  */ 
  public static connection getconnection() throws sqlexception{
    return drivermanager.getconnection(url, username,password);
  }
  
  /**
  * @method: release
  * @description: 释放资源,
  *   要释放的资源包括connection数据库连接对象,负责执行sql命令的statement对象,存储查询结果的resultset对象
  * @anthor:孤傲苍狼
  *
  * @param conn
  * @param st
  * @param rs
  */ 
  public static void release(connection conn,statement st,resultset rs){
    if(rs!=null){
      try{
        //关闭存储查询结果的resultset对象
        rs.close();
      }catch (exception e) {
        e.printstacktrace();
      }
      rs = null;
    }
    if(st!=null){
      try{
        //关闭负责执行sql命令的statement对象
        st.close();
      }catch (exception e) {
        e.printstacktrace();
      }
    }
    
    if(conn!=null){
      try{
        //关闭connection数据库连接对象
        conn.close();
      }catch (exception e) {
        e.printstacktrace();
      }
    }
  }
}

三、使用jdbc处理mysql的大文本

对于mysql中的text类型,可调用如下方法设置

preparedstatement.setcharacterstream(index, reader, length);//注意length长度须设置,并且设置为int型

对mysql中的text类型,可调用如下方法获取

reader = resultset. getcharacterstream(string columnlabel);2 string s = resultset.getstring(string columnlabel);

3.1、 测试范例

1、编写sql测试脚本

create database jdbcstudy;
use jdbcstudy;
create table testclob
(
     id int primary key auto_increment,
     resume text
);

2、编写测试代码如下:

package me.gacl.demo;

import java.io.file;
import java.io.filereader;
import java.io.filewriter;
import java.io.reader;
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import me.gacl.utils.jdbcutils;
import org.junit.test;

/**
* @classname: jdbcoperaclob
* @description: 使用jdbc操作mysql的大文本
* @author: 孤傲苍狼
* @date: 2014-9-19 下午10:10:04
*
*/ 
public class jdbcoperaclob {

  /**
  * @method: add
  * @description:向数据库中插入大文本数据
  * @anthor:孤傲苍狼
  *
  */ 
  @test
  public void add(){
    connection conn = null;
    preparedstatement st = null;
    resultset rs = null;
    reader reader = null;
    try{
      conn = jdbcutils.getconnection();
      string sql = "insert into testclob(resume) values(?)";
      st = conn.preparestatement(sql);
      //这种方式获取的路径,其中的空格会被使用“%20”代替
      string path = jdbcoperaclob.class.getclassloader().getresource("data.txt").getpath();
      //将“%20”替换回空格
      path = path.replaceall("%20", " ");
      file file = new file(path);
      reader = new filereader(file);
      st.setcharacterstream(1, reader,(int) file.length());
      int num = st.executeupdate();
      if(num>0){
        system.out.println("插入成功!!");
      }
      //关闭流
      reader.close();
    }catch (exception e) {
      e.printstacktrace();
    }finally{
      jdbcutils.release(conn, st, rs);
    }
  }
  
  /**
  * @method: read
  * @description: 读取数据库中的大文本数据
  * @anthor:孤傲苍狼
  *
  */ 
  @test
  public void read(){
    connection conn = null;
    preparedstatement st = null;
    resultset rs = null;
    try{
      conn = jdbcutils.getconnection();
      string sql = "select resume from testclob where id=2";
      st = conn.preparestatement(sql);
      rs = st.executequery();
      
      string contentstr ="";
      string content = "";
      if(rs.next()){
        //使用resultset.getstring("字段名")获取大文本数据的内容
        content = rs.getstring("resume");
        //使用resultset.getcharacterstream("字段名")获取大文本数据的内容
        reader reader = rs.getcharacterstream("resume");
        char buffer[] = new char[1024];
        int len = 0;
        filewriter out = new filewriter("d:\\1.txt");
        while((len=reader.read(buffer))>0){
          contentstr += new string(buffer);
          out.write(buffer, 0, len);
        }
        out.close();
        reader.close();
      }
      system.out.println(content);
      system.out.println("-----------------------------------------------");
      system.out.println(contentstr);
    }catch (exception e) {
      e.printstacktrace();
    }finally{
      jdbcutils.release(conn, st, rs);
    }
  }
}

四、使用jdbc处理mysql的二进制数据

对于mysql中的blob类型,可调用如下方法设置:

preparedstatement. setbinarystream(i, inputstream, length);

对mysql中的blob类型,可调用如下方法获取:

inputstream in = resultset.getbinarystream(string columnlabel);
inputstream in = resultset.getblob(string columnlabel).getbinarystream(); 

4.1、 测试范例

1、编写sql测试脚本

create table testblob
(
   id int primary key auto_increment,
   image longblob
 );

2、编写测试代码如下:

package me.gacl.demo;

import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import me.gacl.utils.jdbcutils;
import org.junit.test;

/**
* @classname: jdbcoperaclob
* @description: 使用jdbc操作mysql的二进制数据(例如图像、声音、二进制文)
* @author: 孤傲苍狼
* @date: 2014-9-19 下午10:10:04
*
*/ 
public class jdbcoperablob {

  /**
  * @method: add
  * @description:向数据库中插入二进制数据
  * @anthor:孤傲苍狼
  *
  */ 
  @test
  public void add(){
    connection conn = null;
    preparedstatement st = null;
    resultset rs = null;
    try{
      conn = jdbcutils.getconnection();
      string sql = "insert into testblob(image) values(?)";
      st = conn.preparestatement(sql);
      //这种方式获取的路径,其中的空格会被使用“%20”代替
      string path = jdbcoperablob.class.getclassloader().getresource("01.jpg").getpath();
      //将“%20”替换会空格
      path = path.replaceall("%20", " ");
      file file = new file(path);
      fileinputstream fis = new fileinputstream(file);//生成的流
      st.setbinarystream(1, fis,(int) file.length());
      int num = st.executeupdate();
      if(num>0){
        system.out.println("插入成功!!");
      }
      fis.close();
    }catch (exception e) {
      e.printstacktrace();
    }finally{
      jdbcutils.release(conn, st, rs);
    }
  }
  
  /**
  * @method: read
  * @description: 读取数据库中的二进制数据
  * @anthor:孤傲苍狼
  *
  */ 
  @test
  public void read() {
    connection conn = null;
    preparedstatement st = null;
    resultset rs = null;
    try {
      conn = jdbcutils.getconnection();
      string sql = "select image from testblob where id=?";
      st = conn.preparestatement(sql);
      st.setint(1, 1);
      rs = st.executequery();
      if (rs.next()) {
        //inputstream in = rs.getblob("image").getbinarystream();//这种方法也可以
        inputstream in = rs.getbinarystream("image");
        int len = 0;
        byte buffer[] = new byte[1024];
        
        fileoutputstream out = new fileoutputstream("d:\\1.jpg");
        while ((len = in.read(buffer)) > 0) {
          out.write(buffer, 0, len);
        }
        in.close();
        out.close();
      }
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      jdbcutils.release(conn, st, rs);
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网