javaweb学习总结(三十四)——使用JDBC处理MySQL大数据
一、基本概念
大數(shù)據(jù)也稱之為LOB(Large Objects),LOB又分為:clob和blob,clob用于存儲大文本,blob用于存儲二進制數(shù)據(jù),例如圖像、聲音、二進制文等。
在實際開發(fā)中,有時是需要用程序把大文本或二進制數(shù)據(jù)直接保存到數(shù)據(jù)庫中進行儲存的。
  對MySQL而言只有blob,而沒有clob,mysql存儲大文本采用的是Text,Text和blob分別又分為:
  TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT
  TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB
二、搭建測試環(huán)境
2.1、搭建的測試項目架構
如下:
2.2、編寫db.properties配置文件
1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/jdbcStudy 3 username=root 4 password=XDP2.3、編寫JdbcUtils工具類
1 package me.gacl.utils;2 3 import java.io.InputStream;4 import java.sql.Connection;5 import java.sql.DriverManager;6 import java.sql.ResultSet;7 import java.sql.SQLException;8 import java.sql.Statement;9 import java.util.Properties; 10 11 public class JdbcUtils { 12 13 private static String driver = null; 14 private static String url = null; 15 private static String username = null; 16 private static String password = null; 17 18 static{ 19 try{ 20 //讀取db.properties文件中的數(shù)據(jù)庫連接信息 21 InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"); 22 Properties prop = new Properties(); 23 prop.load(in); 24 25 //獲取數(shù)據(jù)庫連接驅動 26 driver = prop.getProperty("driver"); 27 //獲取數(shù)據(jù)庫連接URL地址 28 url = prop.getProperty("url"); 29 //獲取數(shù)據(jù)庫連接用戶名 30 username = prop.getProperty("username"); 31 //獲取數(shù)據(jù)庫連接密碼 32 password = prop.getProperty("password"); 33 34 //加載數(shù)據(jù)庫驅動 35 Class.forName(driver); 36 37 }catch (Exception e) { 38 throw new ExceptionInInitializerError(e); 39 } 40 } 41 42 /** 43 * @Method: getConnection 44 * @Description: 獲取數(shù)據(jù)庫連接對象 45 * @Anthor:孤傲蒼狼 46 * 47 * @return Connection數(shù)據(jù)庫連接對象 48 * @throws SQLException 49 */ 50 public static Connection getConnection() throws SQLException{ 51 return DriverManager.getConnection(url, username,password); 52 } 53 54 /** 55 * @Method: release 56 * @Description: 釋放資源, 57 * 要釋放的資源包括Connection數(shù)據(jù)庫連接對象,負責執(zhí)行SQL命令的Statement對象,存儲查詢結果的ResultSet對象 58 * @Anthor:孤傲蒼狼 59 * 60 * @param conn 61 * @param st 62 * @param rs 63 */ 64 public static void release(Connection conn,Statement st,ResultSet rs){ 65 if(rs!=null){ 66 try{ 67 //關閉存儲查詢結果的ResultSet對象 68 rs.close(); 69 }catch (Exception e) { 70 e.printStackTrace(); 71 } 72 rs = null; 73 } 74 if(st!=null){ 75 try{ 76 //關閉負責執(zhí)行SQL命令的Statement對象 77 st.close(); 78 }catch (Exception e) { 79 e.printStackTrace(); 80 } 81 } 82 83 if(conn!=null){ 84 try{ 85 //關閉Connection數(shù)據(jù)庫連接對象 86 conn.close(); 87 }catch (Exception e) { 88 e.printStackTrace(); 89 } 90 } 91 } 92 }三、使用JDBC處理MySQL的大文本
對于MySQL中的Text類型,可調(diào)用如下方法設置
1 PreparedStatement.setCharacterStream(index, reader, length);//注意length長度須設置,并且設置為int型對MySQL中的Text類型,可調(diào)用如下方法獲取
1 reader = resultSet. getCharacterStream(String columnLabel);2 string s = resultSet.getString(String columnLabel);3.1、 測試范例
1、編寫SQL測試腳本
1 create database jdbcstudy; 2 use jdbcstudy; 3 create table testclob 4 ( 5 id int primary key auto_increment, 6 resume text 7 );2、編寫測試代碼如下:
1 package me.gacl.demo;2 3 import java.io.File;4 import java.io.FileReader;5 import java.io.FileWriter;6 import java.io.Reader;7 import java.sql.Connection;8 import java.sql.PreparedStatement;9 import java.sql.ResultSet;10 import me.gacl.utils.JdbcUtils;11 import org.junit.Test;12 13 /**14 * @ClassName: JdbcOperaClob15 * @Description: 使用JDBC操作MySQL的大文本16 * @author: 孤傲蒼狼17 * @date: 2014-9-19 下午10:10:0418 *19 */ 20 public class JdbcOperaClob {21 22 /**23 * @Method: add24 * @Description:向數(shù)據(jù)庫中插入大文本數(shù)據(jù)25 * @Anthor:孤傲蒼狼26 *27 */ 28 @Test29 public void add(){30 Connection conn = null;31 PreparedStatement st = null;32 ResultSet rs = null;33 Reader reader = null;34 try{35 conn = JdbcUtils.getConnection();36 String sql = "insert into testclob(resume) values(?)";37 st = conn.prepareStatement(sql);38 //這種方式獲取的路徑,其中的空格會被使用“%20”代替39 String path = JdbcOperaClob.class.getClassLoader().getResource("data.txt").getPath();40 //將“%20”替換回空格41 path = path.replaceAll("%20", " ");42 File file = new File(path);43 reader = new FileReader(file);44 st.setCharacterStream(1, reader,(int) file.length());45 int num = st.executeUpdate();46 if(num>0){47 System.out.println("插入成功!!");48 }49 //關閉流50 reader.close();51 }catch (Exception e) {52 e.printStackTrace();53 }finally{54 JdbcUtils.release(conn, st, rs);55 }56 }57 58 /**59 * @Method: read60 * @Description: 讀取數(shù)據(jù)庫中的大文本數(shù)據(jù)61 * @Anthor:孤傲蒼狼62 *63 */ 64 @Test65 public void read(){66 Connection conn = null;67 PreparedStatement st = null;68 ResultSet rs = null;69 try{70 conn = JdbcUtils.getConnection();71 String sql = "select resume from testclob where id=2";72 st = conn.prepareStatement(sql);73 rs = st.executeQuery();74 75 String contentStr ="";76 String content = "";77 if(rs.next()){78 //使用resultSet.getString("字段名")獲取大文本數(shù)據(jù)的內(nèi)容79 content = rs.getString("resume");80 //使用resultSet.getCharacterStream("字段名")獲取大文本數(shù)據(jù)的內(nèi)容81 Reader reader = rs.getCharacterStream("resume");82 char buffer[] = new char[1024];83 int len = 0;84 FileWriter out = new FileWriter("D:\\1.txt");85 while((len=reader.read(buffer))>0){86 contentStr += new String(buffer);87 out.write(buffer, 0, len);88 }89 out.close();90 reader.close();91 }92 System.out.println(content);93 System.out.println("-----------------------------------------------");94 System.out.println(contentStr);95 }catch (Exception e) {96 e.printStackTrace();97 }finally{98 JdbcUtils.release(conn, st, rs);99 } 100 } 101 }四、使用JDBC處理MySQL的二進制數(shù)據(jù)
對于MySQL中的BLOB類型,可調(diào)用如下方法設置:
1 PreparedStatement. setBinaryStream(i, inputStream, length);對MySQL中的BLOB類型,可調(diào)用如下方法獲取:
1 InputStream in = resultSet.getBinaryStream(String columnLabel); 2 InputStream in = resultSet.getBlob(String columnLabel).getBinaryStream();?4.1、 測試范例
1、編寫SQL測試腳本
1 create table testblob 2 ( 3 id int primary key auto_increment, 4 image longblob 5 );2、編寫測試代碼如下:
1 package me.gacl.demo;2 3 import java.io.File;4 import java.io.FileInputStream;5 import java.io.FileOutputStream;6 import java.io.InputStream;7 import java.sql.Connection;8 import java.sql.PreparedStatement;9 import java.sql.ResultSet; 10 import me.gacl.utils.JdbcUtils; 11 import org.junit.Test; 12 13 /** 14 * @ClassName: JdbcOperaClob 15 * @Description: 使用JDBC操作MySQL的二進制數(shù)據(jù)(例如圖像、聲音、二進制文) 16 * @author: 孤傲蒼狼 17 * @date: 2014-9-19 下午10:10:04 18 * 19 */ 20 public class JdbcOperaBlob { 21 22 /** 23 * @Method: add 24 * @Description:向數(shù)據(jù)庫中插入二進制數(shù)據(jù) 25 * @Anthor:孤傲蒼狼 26 * 27 */ 28 @Test 29 public void add(){ 30 Connection conn = null; 31 PreparedStatement st = null; 32 ResultSet rs = null; 33 try{ 34 conn = JdbcUtils.getConnection(); 35 String sql = "insert into testblob(image) values(?)"; 36 st = conn.prepareStatement(sql); 37 //這種方式獲取的路徑,其中的空格會被使用“%20”代替 38 String path = JdbcOperaBlob.class.getClassLoader().getResource("01.jpg").getPath(); 39 //將“%20”替換會空格 40 path = path.replaceAll("%20", " "); 41 File file = new File(path); 42 FileInputStream fis = new FileInputStream(file);//生成的流 43 st.setBinaryStream(1, fis,(int) file.length()); 44 int num = st.executeUpdate(); 45 if(num>0){ 46 System.out.println("插入成功!!"); 47 } 48 fis.close(); 49 }catch (Exception e) { 50 e.printStackTrace(); 51 }finally{ 52 JdbcUtils.release(conn, st, rs); 53 } 54 } 55 56 /** 57 * @Method: read 58 * @Description: 讀取數(shù)據(jù)庫中的二進制數(shù)據(jù) 59 * @Anthor:孤傲蒼狼 60 * 61 */ 62 @Test 63 public void read() { 64 Connection conn = null; 65 PreparedStatement st = null; 66 ResultSet rs = null; 67 try { 68 conn = JdbcUtils.getConnection(); 69 String sql = "select image from testblob where id=?"; 70 st = conn.prepareStatement(sql); 71 st.setInt(1, 1); 72 rs = st.executeQuery(); 73 if (rs.next()) { 74 //InputStream in = rs.getBlob("image").getBinaryStream();//這種方法也可以 75 InputStream in = rs.getBinaryStream("image"); 76 int len = 0; 77 byte buffer[] = new byte[1024]; 78 79 FileOutputStream out = new FileOutputStream("D:\\1.jpg"); 80 while ((len = in.read(buffer)) > 0) { 81 out.write(buffer, 0, len); 82 } 83 in.close(); 84 out.close(); 85 } 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } finally { 89 JdbcUtils.release(conn, st, rs); 90 } 91 } 92 }關于使用JDBC處理MySQL大數(shù)據(jù)的內(nèi)容就總結這么多!
http://www.cnblogs.com/xdp-gacl/p/3982581.html
總結
以上是生活随笔為你收集整理的javaweb学习总结(三十四)——使用JDBC处理MySQL大数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 好日子烟头有黑沙是什么烟
- 下一篇: 前程无忧网上怎么解除绑定的手机号?
