處理大資料對象clob資料和blob資料

來源:互聯網
上載者:User

標籤:mem   values   dal   二進位   else   resultset   sub   long   import   

直接上下代碼:

 1 package com.learn.jdbc.chap06; 2  3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.InputStream; 6 import java.sql.Connection; 7 import java.sql.PreparedStatement; 8  9 import com.learn.jdbc.model.Album;10 import com.learn.jdbc.util.DbUtil;11 12 /**13  * 處理大資料對象clob資料和blob資料--插入14  * @author Administrator15  *16  */17 public class Demo1 {18     private static DbUtil dbUtil=new DbUtil();19     20     private static int addAlbum(Album ab) throws Exception{21         Connection con = dbUtil.getCon();22         String sql="insert into sp_album_test values (null,?,?,?,?,?)";23         PreparedStatement pstmt = con.prepareStatement(sql);24         pstmt.setString(1, ab.getName());25         pstmt.setInt(2, ab.getUid());26         pstmt.setLong(3, ab.getTime());27         28         // 大字元資料(clob資料)插入資料庫--文字檔29         File content = ab.getContent();30         InputStream inputStream = new FileInputStream(content);31         pstmt.setAsciiStream(4, inputStream, content.length());32         33         // 位元據(blob資料)插入資料庫--圖片,電影,音樂34         File pic = ab.getPic();35         InputStream inputStream2 = new FileInputStream(pic);36         pstmt.setBinaryStream(5, inputStream2, pic.length());37         38         int result = pstmt.executeUpdate();39         dbUtil.close(pstmt, con);40         return result;41     }42     43     public static void main(String[] args) throws Exception {44         File content = new File("e:/code.txt");45         File pic     = new File("e:/Koala111.png");46         Album ab = new Album("張三", 1, System.currentTimeMillis(), content, pic);47         int result = addAlbum(ab);48         if(result>0){49             System.out.println("資料插入成功");50         }else{51             System.out.println("資料插入失敗");52         }53     }54 }

 

 1 package com.learn.jdbc.chap06; 2  3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.OutputStream; 6 import java.sql.Blob; 7 import java.sql.Clob; 8 import java.sql.Connection; 9 import java.sql.PreparedStatement;10 import java.sql.ResultSet;11 12 import com.learn.jdbc.util.DbUtil;13 14 15 /**16  * 處理大資料對象clob資料和blob資料--查詢17  * @author Administrator18  *19  */20 public class Demo2 {21     private static DbUtil dbUtil=new DbUtil();22     23     private static void listAlbum(int id) throws Exception{24         Connection con = dbUtil.getCon();25         String sql="select * from sp_album_test where id=?";26         PreparedStatement pstmt = con.prepareStatement(sql);27         pstmt.setInt(1, id);28         ResultSet rs = pstmt.executeQuery();29         30         if(rs.next()){31             String name = rs.getString("name");32             int uid = rs.getInt("uid");33             long time = rs.getLong("add_time");34             // 讀取大字元資料--clob資料35             Clob clob =rs.getClob("content");36             String content = clob.getSubString(1,(int) clob.length());37             38             // 讀取位元據--blob資料39             Blob blob = rs.getBlob("pic");40             OutputStream out = new FileOutputStream(new File("e:/kaola.png"));41             out.write(blob.getBytes(1, (int) blob.length()));42             out.close();43             44             System.out.println("名字:"+name);45             System.out.println("uid: "+uid);46             System.out.println("時間: "+time);47             System.out.println("內容: "+content);48             49             50         }51         52     }53     54     55     public static void main(String[] args) throws Exception {56         listAlbum(1);57     }58 }

 

處理大資料對象clob資料和blob資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.