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;
		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 = ?");
			ps.setObject(1, 1002);
			
			rs = ps.executeQuery();
			while(rs.next()) {
				// Clob需要用流来读取,其他字段直接读取
				System.out.println(rs.getInt("id"));
				
				Clob c = rs.getClob("myInfo");
				// 通过流读取Clob出来(通过流来操作)
				Reader 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 (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
发布了45 篇原创文章 · 获赞 1 · 访问量 5213

猜你喜欢

转载自blog.csdn.net/weixin_42814000/article/details/104597476