JDBC之处理大数据类型

这次以MySQL与Oralce为例。

 

先看表结构把:

Sql代码   收藏代码
  1. ------------------------处理大数对象-----------------  
  2.   
  3. ------------------------MySQL----------------------  
  4. --有4种text类型:tinytext、text、mediumtext和longtext  
  5. create table t_clob  
  6. (  
  7.     id      integer ,  
  8.     resume  longtext,  
  9.     primary key(id)  
  10. );  
  11.   
  12. --有4种blob类型:tinyblob、blob、mediumblob和longblob  
  13. create table t_blob  
  14. (  
  15.     id      integer ,  
  16.     photo   longblob,  
  17.     primary key(id)  
  18. );  
  19.   
  20.   
  21. -----------------------Oracle------------------------  
  22. --clob  
  23. create table t_clob  
  24. (  
  25.     id      integer ,  
  26.     resume  clob,  
  27.     primary key(id)  
  28. );  
  29.   
  30. --blob  
  31. create table t_blob  
  32. (  
  33.     id      integer ,  
  34.     photo   blob,  
  35.     primary key(id)  
  36. );  

 

 

先考虑Clob类型的,下面的代码适用于MySQL与Oralce

 

Java代码   收藏代码
  1. package org.monday.demo;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.PrintWriter;  
  6. import java.io.Reader;  
  7. import java.sql.Connection;  
  8. import java.sql.PreparedStatement;  
  9. import java.sql.ResultSet;  
  10.   
  11. import org.junit.Test;  
  12. import org.monday.util.JdbcUtil;  
  13.   
  14. /** 
  15.  * Clob大文本数据操作 
  16.  */  
  17. public class ClobMain {  
  18.   
  19.     private static String INSERT_CLOB = "insert into t_clob(id,resume)values(?,?)";  
  20.     private static String SELECT_CLOB = "select * from t_clob where id=?";  
  21.   
  22.     @Test  
  23.     public void insert() {  
  24.         Connection conn = null;  
  25.         PreparedStatement pstmt = null;  
  26.         try {  
  27.             File file = new File("src/电子商务.txt");  
  28.             Reader reader = new FileReader(file);  
  29.             conn = JdbcUtil.getConnection();  
  30.             pstmt = conn.prepareStatement(INSERT_CLOB);  
  31.             pstmt.setInt(11);  
  32.             pstmt.setCharacterStream(2, reader, (int) file.length());  
  33.             pstmt.executeUpdate();  
  34.         } catch (Exception e) {  
  35.             throw new RuntimeException(e);  
  36.         } finally {  
  37.             JdbcUtil.release(conn, pstmt, null);  
  38.         }  
  39.     }  
  40.   
  41.     @Test  
  42.     public void read() {  
  43.         Connection conn = null;  
  44.         PreparedStatement pstmt = null;  
  45.         ResultSet rs = null;  
  46.         try {  
  47.             conn = JdbcUtil.getConnection();  
  48.             pstmt = conn.prepareStatement(SELECT_CLOB);  
  49.             pstmt.setInt(11);  
  50.             rs = pstmt.executeQuery();  
  51.             if (rs.next()) {  
  52.                 Reader reader = rs.getCharacterStream("resume");  
  53.                 PrintWriter out = new PrintWriter(System.out); // 打印到控制台  
  54.                 // PrintWriter out= new PrintWriter("src/out.txt"); // 打印到文本  
  55.                 try {  
  56.                     int len = 0;  
  57.                     char[] buffer = new char[1024];  
  58.                     while ((len = reader.read(buffer)) > 0) {  
  59.                         out.write(buffer, 0, len);  
  60.                     }  
  61.                 } finally {  
  62.                     if (reader != null) {  
  63.                         reader.close();  
  64.                     }  
  65.                     out.close();  
  66.                 }  
  67.             }  
  68.         } catch (Exception e) {  
  69.             throw new RuntimeException(e);  
  70.         } finally {  
  71.             JdbcUtil.release(conn, pstmt, rs);  
  72.         }  
  73.     }  
  74. }  

 

 

接下来是Blob类型,这个MySQL与Oracle还是有些不一样的。看仔细了。。

 

Java代码   收藏代码
  1. package org.monday.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.io.OutputStream;  
  8. import java.sql.Connection;  
  9. import java.sql.PreparedStatement;  
  10. import java.sql.ResultSet;  
  11.   
  12. import org.junit.Test;  
  13. import org.monday.util.JdbcUtil;  
  14.   
  15. /** 
  16.  * Blob图片数据操作 
  17.  * MySQL与Oralce有所不同 
  18.  */  
  19. public class BlobMain {  
  20.   
  21.     private static String INSERT_BLOB = "insert into t_blob(id,photo)values(?,?)";  
  22.     private static String SELECT_BLOB = "select * from t_blob where id=?";  
  23.   
  24.     @Test  
  25.     public void insert() {  
  26.         Connection conn = null;  
  27.         PreparedStatement pstmt = null;  
  28.         try {  
  29.             File file = new File("src/王若琳.jpg");  
  30.             InputStream in = new FileInputStream(file);  
  31.             conn = JdbcUtil.getConnection();  
  32.             pstmt = conn.prepareStatement(INSERT_BLOB);  
  33.             pstmt.setInt(11);  
  34.             pstmt.setAsciiStream(2, in, (int) file.length());  
  35.             pstmt.executeUpdate();  
  36.         } catch (Exception e) {  
  37.             throw new RuntimeException(e);  
  38.         } finally {  
  39.             JdbcUtil.release(conn, pstmt, null);  
  40.         }  
  41.     }  
  42.   
  43.     @Test  
  44.     public void read() {  
  45.         Connection conn = null;  
  46.         PreparedStatement pstmt = null;  
  47.         ResultSet rs = null;  
  48.         try {  
  49.             conn = JdbcUtil.getConnection();  
  50.             pstmt = conn.prepareStatement(SELECT_BLOB);  
  51.             pstmt.setInt(11);  
  52.             rs = pstmt.executeQuery();  
  53.             if (rs.next()) {  
  54.                 InputStream in = rs.getBlob("photo").getBinaryStream();  
  55.                 // 或者InputStream in = rs.getAsciiStream("photo"); 但是这个却Oralce不好用  
  56.                 OutputStream out = new FileOutputStream("src/out.jpg");  
  57.                 try {  
  58.                     int len = 0;  
  59.                     byte[] b = new byte[1024];  
  60.                     while ((len = in.read(b)) > 0) {  
  61.                         out.write(b, 0, len);  
  62.                     }  
  63.                 } finally {  
  64.                     if (in != null) {  
  65.                         in.close();  
  66.                     }  
  67.                     if (out != null) {  
  68.                         out.close();  
  69.                     }  
  70.                 }  
  71.             }  
  72.         } catch (Exception e) {  
  73.             throw new RuntimeException(e);  
  74.         } finally {  
  75.             JdbcUtil.release(conn, pstmt, rs);  
  76.         }  
  77.     }  
  78.   
  79.       
  80.     /** 
  81.      * Oralce插入Blob类型很特殊 
  82.      */  
  83.     @Test  
  84.     public void insert_oralce() {  
  85.   
  86.         // 插入Oralce用的  
  87.         String INSERT_BLOB_ORACLE = "insert into t_blob(id,photo)values(?,empty_blob())"// empty_blob()固定的  
  88.         String SELECT_BLOB_ORACLE = "select * from t_blob where id=? for update"// for update 固定的,不可去  
  89.   
  90.         Connection conn = null;  
  91.         PreparedStatement pstmt = null;  
  92.         ResultSet rs = null;  
  93.         try {  
  94.             // 不用开启事务,网上传说要开启事务  
  95.             // conn.setAutoCommit(false);  
  96.             conn = JdbcUtil.getConnection();  
  97.             // 插入一个指针  
  98.             pstmt = conn.prepareStatement(INSERT_BLOB_ORACLE);  
  99.             pstmt.setInt(11);  
  100.             pstmt.executeUpdate();  
  101.   
  102.             // 读取插入的那个指针  
  103.             pstmt = conn.prepareStatement(SELECT_BLOB_ORACLE);  
  104.             pstmt.setInt(11);  
  105.             rs = pstmt.executeQuery();  
  106.             if (rs.next()) {  
  107.                 InputStream in = new FileInputStream(new File("src/王若琳.jpg"));  
  108.                 // 得到java.sql.Blob对象后强制转换为oracle.sql.BLOB  
  109.                 oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("photo");  
  110.                 OutputStream out = blob.getBinaryOutputStream();  
  111.                 try {  
  112.                     byte[] b = new byte[blob.getBufferSize()];  
  113.                     int len;  
  114.                     while ((len = in.read(b)) > 0) {  
  115.                         out.write(b, 0, len);  
  116.                     }  
  117.                 } finally {  
  118.                     if (in != null) {  
  119.                         in.close();  
  120.                     }  
  121.                     if (out != null) {  
  122.                         out.close();  
  123.                     }  
  124.                 }  
  125.             }  
  126.         } catch (Exception e) {  
  127.             throw new RuntimeException(e);  
  128.         } finally {  
  129.             JdbcUtil.release(conn, pstmt, rs);  
  130.         }  
  131.     }  
  132. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值