大数据的存取

1. 大文本的存取

   
   
  1. package com.cn.lob;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.Reader;
  6. import java.sql.Connection;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import org.junit.Test;
  10. import com.cn.Util.JdbcUtil;
  11. /**
  12. * Author:Liu Zhiyong(QQ:1012421396)
  13. * Version:Version_1
  14. * Date:2017年8月3日18:43:27
  15. * Desc:大文本的存取
  16. use mydb
  17. create table lob(
  18. id int primary key,
  19. content longtext
  20. );
  21. */
  22. public class ClobDemo {
  23. @Test
  24. public void testAdd() throws Exception {
  25. Connection conn = null;
  26. PreparedStatement pstmt = null;
  27. try{
  28. conn = JdbcUtil.getConnection();
  29. String sql = "insert into lob(id, content) values(?, ?)";
  30. pstmt = conn.prepareStatement(sql);
  31. pstmt.setInt(1, 3);
  32. //大数据要使用流的形式
  33. File file = new File("d:/a.txt");
  34. FileReader reader = new FileReader(file);
  35. pstmt.setCharacterStream(2, reader, file.length());
  36. int flag = pstmt.executeUpdate();
  37. if(flag > 0){
  38. System.out.println("插入成功");
  39. }
  40. }catch(Exception e){
  41. throw new RuntimeException(e);
  42. }finally{
  43. JdbcUtil.close(conn, pstmt, null);
  44. }
  45. }
  46. @Test
  47. public void testRead() throws Exception {
  48. Connection conn = null;
  49. PreparedStatement pstmt = null;
  50. ResultSet rs = null;
  51. try{
  52. conn = JdbcUtil.getConnection();
  53. String sql = "select * from lob where id = ?";
  54. pstmt = conn.prepareStatement(sql);
  55. pstmt.setInt(1, 2);
  56. //大数据要使用流的形式
  57. File file = new File("d:/b.txt");
  58. rs = pstmt.executeQuery();
  59. if(rs.next()){
  60. Reader reader = rs.getCharacterStream("content");
  61. FileWriter writer = new FileWriter(file);
  62. char[] buff= new char[1024];
  63. int len = -1;
  64. while((len = reader.read(buff)) != -1){
  65. writer.write(buff, 0, len);
  66. }
  67. writer.close();
  68. reader.close();
  69. }
  70. }catch(Exception e){
  71. throw new RuntimeException(e);
  72. }finally{
  73. JdbcUtil.close(conn, pstmt, rs);
  74. }
  75. }
  76. }

2. 大二进制数据的存取

   
   
  1. package com.cn.lob;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import org.junit.Test;
  11. import com.cn.Util.JdbcUtil;
  12. /**
  13. * Author:Liu Zhiyong(QQ:1012421396)
  14. * Version:Version_1
  15. * Date:2017年8月3日19:27:19
  16. * Desc:大二进制数据的存取
  17. use mydb
  18. create table lob2(
  19. id int primary key,
  20. content longblob
  21. );
  22. */
  23. public class BlobDemo {
  24. @Test
  25. public void testAdd() throws Exception {
  26. Connection conn = null;
  27. PreparedStatement pstmt = null;
  28. try{
  29. conn = JdbcUtil.getConnection();
  30. String sql = "insert into lob2(id, content) values(?, ?)";
  31. pstmt = conn.prepareStatement(sql);
  32. pstmt.setInt(1, 3);
  33. //大数据要使用流的形式
  34. InputStream in = new FileInputStream("d:/me.jpg");
  35. pstmt.setBinaryStream(2, in, in.available());
  36. int flag = pstmt.executeUpdate();
  37. if(flag > 0){
  38. System.out.println("插入成功");
  39. }
  40. }catch(Exception e){
  41. throw new RuntimeException(e);
  42. }finally{
  43. JdbcUtil.close(conn, pstmt, null);
  44. }
  45. }
  46. @Test
  47. public void testRead() throws Exception {
  48. Connection conn = null;
  49. PreparedStatement pstmt = null;
  50. ResultSet rs = null;
  51. try{
  52. conn = JdbcUtil.getConnection();
  53. String sql = "select * from lob2 where id = ?";
  54. pstmt = conn.prepareStatement(sql);
  55. pstmt.setInt(1, 3);
  56. //大数据要使用流的形式
  57. File file = new File("d:/b.txt");
  58. rs = pstmt.executeQuery();
  59. if(rs.next()){
  60. InputStream in = rs.getBinaryStream("content");
  61. OutputStream out = new FileOutputStream("d:/me2.jpg");
  62. byte[] buff= new byte[1024];
  63. int len = -1;
  64. while((len = in.read(buff)) != -1){
  65. out.write(buff, 0, len);
  66. }
  67. out.close();
  68. in.close();
  69. }
  70. }catch(Exception e){
  71. throw new RuntimeException(e);
  72. }finally{
  73. JdbcUtil.close(conn, pstmt, rs);
  74. }
  75. }
  76. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值