javaweb學(xué)習(xí)總結(jié)——使用JDBC處理MySQL大數(shù)據(jù)
BLOB (binary large object),二進(jìn)制大對象,是一個可以存儲二進(jìn)制文件的容器。在計(jì)算機(jī)中,BLOB常常是數(shù)據(jù)庫中用來存儲二進(jìn)制文件的字段類型,BLOB是一個大文件,典型的BLOB是一張圖片或一個聲音文件,由于它們的尺寸,必須使用特殊的方式來處理(例如:上傳、下載或者存放到一個數(shù)據(jù)庫)。
一、基本概念
在實(shí)際開發(fā)中,有時是需要用程序把大文本或二進(jìn)制數(shù)據(jù)直接保存到數(shù)據(jù)庫中進(jìn)行儲存的。
對MySQL而言只有blob,而沒有clob,mysql存儲大文本采用的是Text,Text和blob分別又分為:
TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT
TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB
二、搭建測試環(huán)境
2.1、搭建的測試項(xiàng)目架構(gòu)
如圖:
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文件中的數(shù)據(jù)庫連接信息 InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"); Properties prop = new Properties(); prop.load(in); //獲取數(shù)據(jù)庫連接驅(qū)動 driver = prop.getProperty("driver"); //獲取數(shù)據(jù)庫連接URL地址 url = prop.getProperty("url"); //獲取數(shù)據(jù)庫連接用戶名 username = prop.getProperty("username"); //獲取數(shù)據(jù)庫連接密碼 password = prop.getProperty("password"); //加載數(shù)據(jù)庫驅(qū)動 Class.forName(driver); }catch (Exception e) { throw new ExceptionInInitializerError(e); } } /** * @Method: getConnection * @Description: 獲取數(shù)據(jù)庫連接對象 * @Anthor:孤傲蒼狼 * * @return Connection數(shù)據(jù)庫連接對象 * @throws SQLException */ public static Connection getConnection() throws SQLException{ return DriverManager.getConnection(url, username,password); } /** * @Method: release * @Description: 釋放資源, * 要釋放的資源包括Connection數(shù)據(jù)庫連接對象,負(fù)責(zé)執(zhí)行SQL命令的Statement對象,存儲查詢結(jié)果的ResultSet對象 * @Anthor:孤傲蒼狼 * * @param conn * @param st * @param rs */ public static void release(Connection conn,Statement st,ResultSet rs){ if(rs!=null){ try{ //關(guān)閉存儲查詢結(jié)果的ResultSet對象 rs.close(); }catch (Exception e) { e.printStackTrace(); } rs = null; } if(st!=null){ try{ //關(guān)閉負(fù)責(zé)執(zhí)行SQL命令的Statement對象 st.close(); }catch (Exception e) { e.printStackTrace(); } } if(conn!=null){ try{ //關(guān)閉Connection數(shù)據(jù)庫連接對象 conn.close(); }catch (Exception e) { e.printStackTrace(); } } } }
三、使用JDBC處理MySQL的大文本
對于MySQL中的Text類型,可調(diào)用如下方法設(shè)置
PreparedStatement.setCharacterStream(index, reader, length);//注意length長度須設(shè)置,并且設(shè)置為int型
對MySQL中的Text類型,可調(diào)用如下方法獲取
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:向數(shù)據(jù)庫中插入大文本數(shù)據(jù) * @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("插入成功??!"); } //關(guān)閉流 reader.close(); }catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtils.release(conn, st, rs); } } /** * @Method: read * @Description: 讀取數(shù)據(jù)庫中的大文本數(shù)據(jù) * @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("字段名")獲取大文本數(shù)據(jù)的內(nèi)容 content = rs.getString("resume"); //使用resultSet.getCharacterStream("字段名")獲取大文本數(shù)據(jù)的內(nèi)容 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的二進(jìn)制數(shù)據(jù)
對于MySQL中的BLOB類型,可調(diào)用如下方法設(shè)置:
PreparedStatement. setBinaryStream(i, inputStream, length);
對MySQL中的BLOB類型,可調(diào)用如下方法獲取:
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的二進(jìn)制數(shù)據(jù)(例如圖像、聲音、二進(jìn)制文) * @author: 孤傲蒼狼 * @date: 2014-9-19 下午10:10:04 * */ public class JdbcOperaBlob { /** * @Method: add * @Description:向數(shù)據(jù)庫中插入二進(jìn)制數(shù)據(jù) * @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: 讀取數(shù)據(jù)庫中的二進(jìn)制數(shù)據(jù) * @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); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JDBC用法小結(jié)
- Java 數(shù)據(jù)庫連接(JDBC)的相關(guān)總結(jié)
- Java JDBC連接數(shù)據(jù)庫常見操作總結(jié)
- 使用JDBC連接Mysql數(shù)據(jù)庫會出現(xiàn)的問題總結(jié)
- JDBC連接mysql亂碼異常問題處理總結(jié)
- JDBC常用接口總結(jié)
- Java中JDBC事務(wù)與JTA分布式事務(wù)總結(jié)與區(qū)別
- java開發(fā)中基于JDBC連接數(shù)據(jù)庫實(shí)例總結(jié)
- JDBC連接Sql Server 2005總結(jié)
- 淺析JAVA常用JDBC連接數(shù)據(jù)庫的方法總結(jié)
- JDBC數(shù)據(jù)庫的使用操作總結(jié)
- JDBC的擴(kuò)展知識點(diǎn)總結(jié)
相關(guān)文章
淺析Java進(jìn)制轉(zhuǎn)換、輸入、命名問題
這篇文章主要介紹了Java進(jìn)制轉(zhuǎn)換、輸入、命名問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07idea中怎樣創(chuàng)建并運(yùn)行第一個java程序
這篇文章主要介紹了idea中怎樣創(chuàng)建并運(yùn)行第一個java程序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08struts2標(biāo)簽總結(jié)_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)總結(jié)了struts2標(biāo)簽的使用方法,和學(xué)習(xí)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09SpringBoot環(huán)境搭建及第一個程序運(yùn)行(小白教程)
這篇文章主要介紹了SpringBoot環(huán)境搭建及第一個程序運(yùn)行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06java父子節(jié)點(diǎn)parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整
這篇文章主要介紹了java父子節(jié)點(diǎn)parentid樹形結(jié)構(gòu)數(shù)據(jù)的規(guī)整,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07