JDBC—CLOB 文本大对象的使用

CLOB(Character Large Object)
  – 用于存储大量的文本数据
  – 大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式来处理的。而非一般的字段,一次即可读出数据。
 
Mysql中相关类型:
  – TINYTEXT最大长度为255字符的TEXT列。
  – TEXT[(M)]最大长度为65,535字符的TEXT列。
  – MEDIUMTEXT最大长度为16,777,215字符的TEXT列。
  – LONGTEXT最大长度为4,294,967,295或4GB字符的TEXT列。

将字符串、文件内容插入数据库中的CLOB字段
/**
 * CLOB通过流来操作
 * 测试CLOB  文本大对象的使用
 * 包含:将字符串、文件内容插入数据库中的CLOB字段、将CLOB字段值取出来的操作。
 * 将字符串、文件内容插入数据库中的CLOB字段
 */
public class Demo09 {

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			
			ps = conn.prepareStatement("insert into t_user1 (username,myInfo) values (?,?)");
			ps.setString(1, "林伟茂");
			// 第二个参数为流
//			ps.setClob(2, new FileReader(new File("d:/a.txt")));  // 将文本文件内容直接输入到数据库中
		
			// 将程序中的字符串输入到数据库的CLOB字段中
			// 将字符串以流的方式输进去
			ps.setClob(2, new BufferedReader(new InputStreamReader(new ByteArrayInputStream("aaaaabbbbb".getBytes()))));
			
			
			ps.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
将CLOB字段值从数据库中取出来的操作
/**
 * CLOB通过流来操作
 * 测试CLOB  文本大对象的使用
 * 包含:将字符串、文件内容插入数据库中的CLOB字段、将CLOB字段值取出来的操作。
 * 将CLOB字段值从数据库中取出来的操作
 */
public class Demo09_1 {

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Reader r = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			
//			ps = conn.prepareStatement("insert into t_user1 (username,myInfo) values (?,?)");
//			ps.setString(1, "林伟茂");
//			// 第二个参数为流
			ps.setClob(2, new FileReader(new File("d:/a.txt")));  // 将文本文件内容直接输入到数据库中
//		
//			// 将程序中的字符串输入到数据库的CLOB字段中
//			// 将字符串以流的方式输进去
//			ps.setClob(2, new BufferedReader(new InputStreamReader(new ByteArrayInputStream("aaaaabbbbb".getBytes()))));
			
			ps = conn.prepareStatement("select * from t_user1 where id = ?");
			// 这里的 1002(id)为你在数据库存储的id号(储存时的id号),需要到数据库看一下 
			ps.setObject(1, 1002);
			
			rs = ps.executeQuery();
			while(rs.next()) {
				// Clob需要用流来读取,其他字段直接读取
				System.out.println(rs.getInt("id"));
				
				Clob c = rs.getClob("myInfo");
				// 通过流读取Clob出来(通过流来操作)
				r = c.getCharacterStream();  // get字符流
				int temp = 0;
				// 等于 -1代表到最后了
				while((temp=r.read())!=-1) {
					System.out.print((char)temp);
				}
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (r != null) {
					r.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值