日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

6、处理大数据对象(CLOB 数据小说+BLOG 数据图片,电影,音乐)

發布時間:2025/3/20 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 6、处理大数据对象(CLOB 数据小说+BLOG 数据图片,电影,音乐) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大數據對象處理主要有 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);}}

運行結果:

總結

以上是生活随笔為你收集整理的6、处理大数据对象(CLOB 数据小说+BLOG 数据图片,电影,音乐)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。