大数据对象处理主要有 CLOB(character large object)和 BLOB(binary large object)两种类型的字段
第一节:处理 CLOB 数据
在 CLOB中可以存储大字符数据对象,比如长篇小说;
第二节:处理 BLOG 数据
在 BLOB 中可以存放二进制大数据对象,比如图片,电影,音乐;
实例1:通过流的方式把CLOB数据插入数据表内
1、图书Books模型
public class Books {
private int id;
private String bookName;
private String author;
private float price;
private File content;
public Books() {
super();
// TODO Auto-generated constructor stub
}
public Books(String bookName, String author, float price, File content) {
super();
this.bookName = bookName;
this.author = author;
this.price = price;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public File getContent() {
return content;
}
public void setContent(File content) {
this.content = content;
}
}
2、工具类
public class DbUtil {
private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
private static String dbUserName="root";
private static String dbPassword="root";
private static String jdbcName="com.mysql.jdbc.Driver";
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
return con;
}
/**
* 关闭连接
* @param con
* @throws Exception
*/
public void close(Statement stmt,Connection con)throws Exception{
if(stmt!=null){
stmt.close();
if(con!=null){
con.close();
}
}
}
}
3、测试类
public class jdbcTest {
private static DbUtil dbUtil=new DbUtil();
private static Books books=new Books();
private static int addBook(Books books)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="insert into books values(null,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, books.getBookName());
pstmt.setString(2, books.getAuthor());
pstmt.setFloat(3, books.getPrice());
-------输入流获取文件--------------------------------------
File context=books.getContent();//获取文件
InputStream inputStream=new FileInputStream(context);
pstmt.setAsciiStream(4, inputStream,context.length());//给第5个?设置值
----------------------------------------------------
int result=pstmt.executeUpdate();
dbUtil.close(pstmt, con);
return result;
}
public static void main(String[] args) throws Exception {
File context=new File("E:/luguo.txt");
Books books=new Books("从你的全世界路过","张嘉佳",39.9f,context);
addBook(books);
}
}
运行结果:
实例2:通过输出流读取文本
3、测试类
public class jdbcTest {
private static DbUtil dbUtil=new DbUtil();
private static Books books=new Books();
private static void getBook(int id)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="select * from books where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
String bookName=rs.getString("bookName");
String author=rs.getString("author");
float price=rs.getFloat("price");
Clob c=rs.getClob("content");//根据给定的参数名称,检索指定 JDBC BLOB 参数作为 Java 编程语言中的 Clob 对象的值。
String context=c.getSubString(1, (int)c.length());
System.out.println("书名:"+bookName);
System.out.println("作者:"+author);
System.out.println("价格:"+price);
System.out.println("内容:"+context);
}
dbUtil.close(pstmt, con);
}
public static void main(String[] args) throws Exception {
getBook(24);
}
}
运行结果:
实例3:处理BLOG数据(二进制),通过输入流插入图片到数据表
1、图书Books模型
public class Books {
private int id;
private String bookName;
private String author;
private float price;
private File content;
private File illustration;
public Books() {
super();
// TODO Auto-generated constructor stub
}
public Books(String bookName, String author, float price, File content) {
super();
this.bookName = bookName;
this.author = author;
this.price = price;
this.content = content;
}
public Books(String bookName, String author, float price, File content, File illustration) {
super();
this.bookName = bookName;
this.author = author;
this.price = price;
this.content = content;
this.illustration = illustration;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public File getContent() {
return content;
}
public void setContent(File content) {
this.content = content;
}
public File getIllustration() {
return illustration;
}
public void setIllustration(File illustration) {
this.illustration = illustration;
}
}
2、工具类
public class DbUtil {
private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";
private static String dbUserName="root";
private static String dbPassword="root";
private static String jdbcName="com.mysql.jdbc.Driver";
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
return con;
}
/**
* 关闭连接
* @param con
* @throws Exception
*/
public void close(Statement stmt,Connection con)throws Exception{
if(stmt!=null){
stmt.close();
if(con!=null){
con.close();
}
}
}
}
3、测试类
public class jdbcTest {
private static DbUtil dbUtil=new DbUtil();
private static Books books=new Books();
private static int addBooks(Books books)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="insert into books values(null,?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, books.getBookName());
pstmt.setString(2, books.getAuthor());
pstmt.setFloat(3, books.getPrice());
File context=books.getContent();
InputStream inputStream=new FileInputStream(context);
pstmt.setAsciiStream(4, inputStream,context.length());
File illustration=books.getIllustration();//获取图片文件
InputStream inputStream2=new FileInputStream(context);
pstmt.setBinaryStream(5, inputStream2,illustration.length());//二进制,给第六个?赋值
int result=pstmt.executeUpdate();
dbUtil.close(pstmt, con);
return result;
}
public static void main(String[] args) throws Exception {
File context=new File("E:/luguo.txt");
File illustration=new File("E:/luguo.jpg");
Books books=new Books("从你的全世界路过","张嘉佳",39.9f,context,illustration);
addBooks(books);
}
}
运行结果:
实例4:通过输出流读取BLOG数据,把图片导入到C盘查看(页面的话直接输出到页面)
3、测试类
public class jdbcTest {
private static DbUtil dbUtil=new DbUtil();
private static Books books=new Books();
private static void getBook(int id)throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="select * from books where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
String bookName=rs.getString("bookName");
String author=rs.getString("author");
float price=rs.getFloat("price");
Clob c=rs.getClob("content");//根据给定的参数名称,检索指定 JDBC BLOB 参数作为 Java 编程语言中的 Clob 对象的值。
String context=c.getSubString(1, (int)c.length());
Blob b=rs.getBlob("illustration");
FileOutputStream out=new FileOutputStream(new File("C:/luguo.jpg"));
out.write(b.getBytes(1, (int)b.length()));
out.close();
System.out.println("书名:"+bookName);
System.out.println("作者:"+author);
System.out.println("价格:"+price);
System.out.println("内容:"+context);
}
dbUtil.close(pstmt, con);
}
public static void main(String[] args) throws Exception {
getBook(26);
}
}
运行结果: