处理大数据对象:处理CLOB数据
来源:互联网 发布:淘宝刷好评兼职平台 编辑:程序博客网 时间:2024/05/08 21:48
大对象的处理主要指CLOB和BLOB两种类型的字段。
在CLOB中可以存储海量的文字,在BLOB中可以存储二进制数据,如图片、电影。
Clob和AsciiStream一般返回的是大文本,而Blob和BinaryStream返回的是二进制数据。
处理CLOB数据
创建数据表:
Create table userclob( id INT AUTO_INCREMENY PRIMARY KEY, name VARCHAR(30) NOT NULL, note LONGTEXT);写入大文本数据:
import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.PreparedStatement ;import java.io.File ;import java.io.FileInputStream ;import java.io.InputStream ;public class ClobDemo01{// 定义MySQL的数据库驱动程序public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;// 定义MySQL数据库的连接地址public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;// MySQL数据库的连接用户名public static final String DBUSER = "root" ;// MySQL数据库的连接密码public static final String DBPASS = "" ;public static void main(String args[]) throws Exception{// 所有异常抛出Connection conn = null ;// 数据库连接PreparedStatement pstmt = null ;String name = "李兴华" ;// 表示姓名String sql = "INSERT INTO userclob(name,note) VALUES (?,?) " ;Class.forName(DBDRIVER) ;// 加载驱动程序conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;pstmt = conn.prepareStatement(sql) ;// 创建PreapredStatement对象File f = new File("d:" + File.separator + "mldn.txt") ;InputStream input = null ;input = new FileInputStream(f) ;// 通过输入流读取文件pstmt.setString(1,name) ;pstmt.setAsciiStream(2,input,(int)f.length()) ;pstmt.executeUpdate() ;conn.close() ;// 数据库关闭}};读取大文本数据:
import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.PreparedStatement ;import java.sql.ResultSet ;import java.io.File ;import java.io.FileInputStream ;import java.io.InputStream ;import java.util.Scanner ;public class ClobDemo02{// 定义MySQL的数据库驱动程序public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;// 定义MySQL数据库的连接地址public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;// MySQL数据库的连接用户名public static final String DBUSER = "root" ;// MySQL数据库的连接密码public static final String DBPASS = "" ;public static void main(String args[]) throws Exception{// 所有异常抛出Connection conn = null ;// 数据库连接PreparedStatement pstmt = null ;ResultSet rs = null ;int id = 1 ;// 读取的编号String sql = "SELECT name,note FROM userclob WHERE id=? " ;Class.forName(DBDRIVER) ;// 加载驱动程序conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;pstmt = conn.prepareStatement(sql) ;// 创建PreapredStatement对象pstmt.setInt(1,id) ;rs = pstmt.executeQuery() ;if(rs.next()){String name = rs.getString(1) ;StringBuffer note = new StringBuffer() ;System.out.println("姓名:" + name) ;InputStream input = rs.getAsciiStream(2) ;Scanner scan = new Scanner(input) ;// 使用Scanner类读取内容scan.useDelimiter("\r\n") ;// 将文件换行作为分割符while(scan.hasNext()){note.append(scan.next()).append("\n") ;}System.out.println("内容:" + note) ;input.close() ;}rs.close() ;pstmt.close() ;conn.close() ;// 数据库关闭}};
使用Clob读取内容:
import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.SQLException ;import java.sql.PreparedStatement ;import java.sql.Clob ;import java.sql.ResultSet ;public class ClobDemo03{// 定义MySQL的数据库驱动程序public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;// 定义MySQL数据库的连接地址public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;// MySQL数据库的连接用户名public static final String DBUSER = "root" ;// MySQL数据库的连接密码public static final String DBPASS = "" ;public static void main(String args[]) throws Exception{// 所有异常抛出Connection conn = null ;// 数据库连接PreparedStatement pstmt = null ;ResultSet rs = null ;int id = 1 ;// 读取的编号String sql = "SELECT name,note FROM userclob WHERE id=? " ;Class.forName(DBDRIVER) ;// 加载驱动程序conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;pstmt = conn.prepareStatement(sql) ;// 创建PreapredStatement对象pstmt.setInt(1,id) ;rs = pstmt.executeQuery() ;if(rs.next()){String name = rs.getString(1) ;System.out.println("姓名:" + name) ;Clob c = rs.getClob(2) ;String note = c.getSubString(1,(int)c.length()) ;System.out.println("内容:" + note ) ;c.truncate(100) ;// 只能读100个内容System.out.println("部分读取内容:" + c.getSubString(1,(int)c.length())) ;}rs.close() ;pstmt.close() ;conn.close() ;// 数据库关闭}};
0 0
- 处理大对象数据-处理CLOB数据
- 处理大数据对象:处理CLOB数据
- Clob处理大数据
- java数据库编程--处理大对象数据(处理CLOB数据)
- JDBC处理大文本数据-Clob
- CLOB大数据对象
- 处理大数据对象 -处理BLOB数据
- JAVA处理Clob大对象
- 使用Hibernate+Oracle9i R2 处理Clob大文本数据
- Mybatis -处理clob类型数据
- java处理Blob、Clob大对象
- Java处理大对象Clob、Blob
- java处理Blob、Clob大对象
- Java处理大对象Clob、Blob
- Spring 数据库处理Clob/Blob大对象
- pb处理图片等大数据对象
- 数据转触时clob字段的处理办法
- spirng 对clob blob数据的处理
- UPX elf to mem
- 字符串的全排列和组合问题整理
- 忽略performSelector警告
- 数组指针和指针数组的区别
- Android开发学习笔记:Intent的简介以及属性的详解
- 处理大数据对象:处理CLOB数据
- Google Java编程风格指南 ()中文版)
- Debug---Eclipse断点调试基础
- BSON与JSON的区别
- 使用PickView实现日期选择器
- sort冒泡
- DenseCap解读
- Git 学习笔记二
- hive和hbase集成异常