6、处理大数据对象(CLOB 数据小说+BLOG 数据图片,电影,音乐)
生活随笔
收集整理的這篇文章主要介紹了
6、处理大数据对象(CLOB 数据小说+BLOG 数据图片,电影,音乐)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
大數(shù)據對象處理主要有 CLOB(character large object)和 BLOB(binary large object)兩種類型的字段
第一節(jié):處理 CLOB 數(shù)據
在 CLOB中可以存儲大字符數(shù)據對象,比如長篇小說;
第二節(jié):處理 BLOG 數(shù)據
在 BLOB 中可以存放二進制大數(shù)據對象,比如圖片,電影,音樂;
實例1:通過流的方式把CLOB數(shù)據插入數(shù)據表內
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";/*** 獲取數(shù)據庫連接* @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");//根據給定的參數(shù)名稱,檢索指定 JDBC BLOB 參數(shù)作為 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數(shù)據(二進制),通過輸入流插入圖片到數(shù)據表
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";/*** 獲取數(shù)據庫連接* @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數(shù)據,把圖片導入到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");//根據給定的參數(shù)名稱,檢索指定 JDBC BLOB 參數(shù)作為 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);}}運行結果:
總結
以上是生活随笔為你收集整理的6、处理大数据对象(CLOB 数据小说+BLOG 数据图片,电影,音乐)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4、使用PreparedStatemen
- 下一篇: 7、使用CallableStatemen