需求:需要在数据库存入大量文本(比如文章、博客等),并且读取。
数据库:
涉及到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);
}
}
}