本文共 3845 字,大约阅读时间需要 12 分钟。
1、创建数据库测试表
DROP TABLE IF EXISTS `clob_test`;CREATE TABLE `clob_test` ( `id` int(10) NOT NULL AUTO_INCREMENT, `big_text` text COMMENT '大文本', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
2、JdbcUtils.java中的代码
package cn.itcast.jdbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * Jdbc工具类 */public final class JdbcUtils { //省略localhost:3306 private static String url = "jdbc:mysql:///jdbc"; private static String username = "root"; private static String password = "root"; /** * 构造器私用,防止直接创建对象, * 当然通过反射可以创建 */ private JdbcUtils(){ } //保证只是注册一次驱动 static{ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } /** * 获取连接 * @return * @throws SQLException */ public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } /** * 释放资源 */ public static void free(ResultSet rs, Statement st, Connection conn) { //规范的关系连接的方式 try{ if(rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ try{ if(st != null) { st.close(); } } catch (SQLException e) { e.printStackTrace(); }finally { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }}3、测试类中的代码
package cn.itcast.jdbc;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;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.sql.Statement;import org.junit.Test;/** * JDBC之大段文本测试 */public class ClobTest { /** * 测试创建 * @throws SQLException * @throws IOException */ @Test public void testCreate() throws SQLException, IOException { create(); } /** * 测试读取 * @throws SQLException * @throws IOException */ @Test public void testRead() throws SQLException, IOException { read(); } /** * 从数据库查询数据 * @throws SQLException * @throws IOException */ static void read() throws SQLException, IOException { Connection conn = null; Statement st = null; ResultSet rs = null; try { //建立连接 conn = JdbcUtils.getConnection(); //创建语句 st = conn.createStatement(); //执行语句 rs = st.executeQuery("select big_text from clob_test"); //处理结果 while(rs.next()) { Clob clob = rs.getClob("big_text"); //获取字符流,这种方式可以 //Reader reader = clob.getCharacterStream(); //这种方式也可以 Reader render = rs.getCharacterStream("big_text"); String s = rs.getString("big_text"); System.out.println(s); File file = new File("JdbcUtils_bak.java"); Writer writer = new BufferedWriter(new FileWriter(file)); char[] buff = new char[1024]; int i = 0; while((i = render.read(buff)) > 0) { writer.write(buff, 0, i); } writer.close(); render.close(); } } finally { JdbcUtils.free(rs, st, conn); } } /** * 从文本文件读取数据存入数据库 * @throws SQLException * @throws IOException */ static void create() throws SQLException, IOException { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ //建立连接 conn = JdbcUtils.getConnection(); //创建语句 String sql = "insert into clob_test(big_text) values(?)"; ps = conn.prepareStatement(sql); File file = new File("src/cn/itcast/jdbc/JdbcUtils.java"); //创建字符流,读取文件 Reader reader = new BufferedReader(new FileReader(file)); ps.setCharacterStream(1, reader, file.length()); //执行语句 int i = ps.executeUpdate(); reader.close(); System.out.println("i=" + i); }finally{ JdbcUtils.free(rs, ps, conn); } }}
转载地址:http://nklvb.baihongyu.com/