【JDBC学习总结】3.JDBC处理mysql大数据

一、大数据

大数据称之为LOB(Large Objects),LOB又分为:clob(character)和blob(binary),clob用于存储大文本,blob用于存储二进制数据,例如图像、声音、二进制文等。

对MySQL而言只有blob,而没有clob,mysql存储大文本采用的是Text
TEXT:TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT
BLOB:TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB


二、TEXT类型的数据的存取

数据库

CREATE TABLE `text` (
  `id` int(11) NOT NULL DEFAULT '0',
  `content` text,
  PRIMARY KEY (`id`)

程序:

package com.jazz.bigdata;

import java.io.*;
import java.sql.*;
import org.junit.*;

public class TestLob {
	@Test
	public void insertText() {
		Connection conn = null;
		PreparedStatement pst = null;
		// ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcstudy", "root", "123456");
			pst = conn.prepareStatement("insert into text(id,content) values(?,?)");
			pst.setInt(1, 1);
			pst.setCharacterStream(2, new FileReader("D:\\me.txt"));

			int num = pst.executeUpdate();
			if (num > 0)
				System.out.print("success");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				pst.close();
				conn.close();
			} catch (Exception e) {

			}
		}

	}
	@Test
	public void readText() {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcstudy", "root", "123456");
			pst = conn.prepareStatement("select content from text where id=1");
			
			rs = pst.executeQuery();
			rs.next();
			Reader cr=rs.getCharacterStream("content");
			char[] c=new char[1024];
			int len;
			while((len=cr.read(c))>0){
				System.out.println(c);
			}
			cr.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				pst.close();
				conn.close();
			} catch (Exception e) {
				
			}
		}
		
	}
}

三、BLOB类型数据的存取

提示:需要使用PreparedStament的setBinaryStream和getBinaryStream方法,同学们自己实现吧!


你可能感兴趣的:(【JDBC学习总结】3.JDBC处理mysql大数据)