博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDBC之大段文本数据的保存与读取
阅读量:2349 次
发布时间:2019-05-10

本文共 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/

你可能感兴趣的文章
最大子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离
查看>>
回文字符序列
查看>>
inline函数必须在头文件中定义吗?
查看>>
内存泄漏检查工具valgrind使用方法
查看>>
Solution of Codility
查看>>
java解析XML的四种方式及比较
查看>>
单例模式(java)详细
查看>>
策略模式(java)
查看>>
java线程中信号量Semaphore类的应用
查看>>
如何设置CentOS为中文显示
查看>>
Nginx配置
查看>>
php-fpm配置
查看>>
Centos 系统时间与当前时间相差和时区解决办法
查看>>
Linux下如何进行FTP设置
查看>>
linux之LVM操作案例
查看>>
由于CentOS的系统安装了epel-release-latest-7.noarch.rpm 导致在使用yum命令时出现Error: xz compression not available问题。
查看>>
php中抽象类和接口的概念与区别
查看>>
php抽象类和接口
查看>>
如何在linux CentOS 上安装chrome 谷歌浏览器
查看>>
laravel5 怎么实现事务
查看>>