java [30] 大文本处理读取和存入clob字段

需求:需要在数据库存入大量文本(比如文章、博客等),并且读取。

数据库:

涉及到clob字段的处理

 

代码:

package com.test1;
/*使用字符流将打大文本放入数据库
 * 
 * 
 * */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class Txtread {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//Create();
		Read(1);
	}
	
	//从数据库读取大文本
	private static void Read(int id) throws IOException {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			//加载驱动建立连接
			conn = jdbcUtils.getConnection();
			//预处理语句
			//String sql = "insert into tst_test(big_txt) values (?)";
			String sql = "select big_txt from tst_test where id =?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			//执行
			rs = ps.executeQuery();
			while (rs.next()) {
				//System.out.print(rs.getClob("big_txt"));
				Clob b = rs.getClob("big_txt");
				String detailinfo = "";
				if (b!=null) {
					detailinfo = b.getSubString(1, (int)b.length());
					System.out.println(detailinfo);
				}
				//System.out.print(rs.getCharacterStream(1));
			}
			
			//System.out.println(rs);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			jdbcUtils.free(rs, ps, conn);
		}
	}
	
	//将文本存入数据库
	private static void Create() throws IOException {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			//加载驱动建立连接
			conn = jdbcUtils.getConnection();
			//预处理语句
			String sql = "insert into tst_test(big_txt) values (?)";
			ps = conn.prepareStatement(sql);
			File file = new File("src/com/test1/Base2.java");
			Reader reader = new BufferedReader(new FileReader(file));
			ps.setCharacterStream(1, reader,(int)file.length());
			//执行
			int i = ps.executeUpdate();
			reader.close();
			System.out.println("i = "+ i);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			jdbcUtils.free(rs, ps, conn);
		}
	}

}

从数据库读取数据写入文本:

/*从数据库读取写入文本
 * 
 * 
 * */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class Txtread {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//Create();
		Read(1);
	}
	
	//从数据库读取大文本
	private static void Read(int id) throws IOException {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			//加载驱动建立连接
			conn = jdbcUtils.getConnection();
			//预处理语句
			//String sql = "insert into tst_test(big_txt) values (?)";
			String sql = "select big_txt from tst_test where id =?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			//执行
			rs = ps.executeQuery();
			while (rs.next()) {
				System.out.print(rs.getClob("big_txt"));
				Clob b = rs.getClob("big_txt");
				//获取数据存入文本
				File file = new File("a.txt");
				Reader reader = b.getCharacterStream();
				Writer writer = new BufferedWriter(new FileWriter(file));
				char[] buff = new char[1024];
				for (int i=0;(i=reader.read(buff))>0;) {
					writer.write(buff, 0, i);
				}
				writer.close();
				reader.close();
				
				//获取数据输出
				/*String detailinfo = "";
				if (b!=null) {
					detailinfo = b.getSubString(1, (int)b.length());
					System.out.println(detailinfo);
				}*/
				//System.out.print(rs.getCharacterStream(1));
			}
			
			//System.out.println(rs);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			jdbcUtils.free(rs, ps, conn);
		}
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值