码迷,mamicode.com
首页 > 数据库 > 详细

用jdbc访问大段文本数据

时间:2014-08-12 03:00:03      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   数据   

bubuko.com,布布扣
 1 package it.cast.jdbc;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileReader;
 8 import java.io.FileWriter;
 9 import java.io.IOException;
10 import java.io.Reader;
11 import java.io.Writer;
12 import java.sql.Clob;
13 import java.sql.Connection;
14 import java.sql.PreparedStatement;
15 import java.sql.ResultSet;
16 import java.sql.SQLException;
17 import java.sql.Statement;
18 
19 public class ClobTest {
20 
21     public static void main(String[] args) throws IOException {
22         read();
23     }
24 
25     static void create() throws IOException {
26         Connection conn = null;
27         PreparedStatement ps = null;
28         ResultSet rs = null;
29 
30         try {
31             // 建立连接
32             conn = jdbcUtils.getConnection();
33 
34             // 创建语句
35             String sql = "insert into clob_test(big_text) values (?)";
36 
37             ps = conn.prepareStatement(sql);
38 
39             File file = new File("src/it/cast/jdbc/Base.java");
40             Reader reader = new BufferedReader(new FileReader(file));
41 
42             ps.setCharacterStream(1, reader, (int) file.length());
43 
44             // 执行语句
45             int i = ps.executeUpdate();
46             reader.close();
47 
48             System.out.println(i);
49 
50         } catch (SQLException e) {
51 
52             e.printStackTrace();
53         } finally {
54             jdbcUtils.free(rs, ps, conn);
55         }
56     }
57 
58     static void read() throws IOException {
59         Connection conn = null;
60         Statement st = null;
61         ResultSet rs = null;
62 
63         try {
64 
65             // 建立连接
66             conn = jdbcUtils.getConnection();
67 
68             // 创建语句
69             st = conn.createStatement();
70 
71             String sql = "select big_text from clob_test";
72 
73             // 执行语句
74             rs = st.executeQuery(sql);
75 
76             while (rs.next()) {
77                 Clob clob = rs.getClob(1);
78                 Reader reader = clob.getCharacterStream();
79                 File file = new File("jdbcUtils_bak.java");
80                 Writer writer = new BufferedWriter(new FileWriter(file));
81                 char[] buff = new char[1024];
82 
83                 for (int i = 0; (i = reader.read(buff)) > 0;) {
84                     writer.write(buff, 0, i);
85                 }
86 
87                 writer.close();
88                 reader.close();
89             }
90 
91         } catch (SQLException e) {
92             e.printStackTrace();
93         } finally {
94             jdbcUtils.free(rs, st, conn);
95         }
96     }
97 
98 }
ClobTest

 

用jdbc访问大段文本数据,布布扣,bubuko.com

用jdbc访问大段文本数据

标签:style   blog   http   color   java   os   io   数据   

原文地址:http://www.cnblogs.com/aineko/p/3906120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!