• 大文本的存取


     1 package cn.itcast.jdbc.lob;
     2 
     3 import java.io.File;
     4 import java.io.FileReader;
     5 import java.io.FileWriter;
     6 import java.io.Reader;
     7 import java.io.Writer;
     8 import java.sql.Connection;
     9 import java.sql.PreparedStatement;
    10 import java.sql.ResultSet;
    11 
    12 import org.junit.Test;
    13 
    14 import cn.itcast.util.JdbcUtil;
    15 //大文本的存取
    16 /*
    17 use day15;
    18 create table t1(
    19     id int primary key,
    20     content longtext
    21 );
    22  */
    23 public class ClobDemo {
    24     @Test
    25     public void testAdd(){
    26         Connection conn = null;
    27         PreparedStatement stmt = null;
    28         try{
    29             conn = JdbcUtil.getConnection();
    30             stmt = conn.prepareStatement("insert into t1 (id,content) values (?,?)");
    31             stmt.setInt(1, 1);
    32             //大数据要使用流的形式
    33             File file = new File("c:/jpm.txt");
    34             Reader reader = new FileReader(file);
    35             stmt.setCharacterStream(2, reader, (int)file.length());//不能使用long的参数,因为mysql根本支持不到那么大的数据,就没有实现
    36             
    37             int i = stmt.executeUpdate();
    38             if(i>0)
    39                 System.out.println("插入成功");
    40             
    41         }catch(Exception e){
    42             e.printStackTrace();
    43         }finally{
    44             JdbcUtil.release(null, stmt, conn);
    45         }
    46     }
    47     @Test
    48     public void testRead(){
    49         Connection conn = null;
    50         PreparedStatement stmt = null;
    51         ResultSet rs = null;
    52         try{
    53             conn = JdbcUtil.getConnection();
    54             stmt = conn.prepareStatement("select * from t1 where id=?");
    55             stmt.setInt(1, 1);
    56             //大数据要使用流的形式
    57             //保存到E盘上
    58             rs = stmt.executeQuery();
    59             if(rs.next()){
    60                 Reader reader = rs.getCharacterStream("content");
    61                 Writer writer = new FileWriter("E:/jpm.txt");
    62                 char c[] = new char[1024];
    63                 int len = -1;
    64                 while((len=reader.read(c))!=-1){
    65                     writer.write(c, 0, len);
    66                 }
    67                 reader.close();
    68                 writer.close();
    69             }
    70             
    71         }catch(Exception e){
    72             e.printStackTrace();
    73         }finally{
    74             JdbcUtil.release(null, stmt, conn);
    75         }
    76     }
    77 }
  • 相关阅读:
    jquery $.ajax $.get $.post的区别
    浅析JQuery中的html(),text(),val()区别
    单词统计续
    第一阶段意见评论
    学习进度9
    第一阶段SCRUM冲刺10
    第一阶段SCRUM冲刺09
    单词统计
    第一阶段SCRUM冲刺08
    学习进度8
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3747922.html
Copyright © 2020-2023  润新知