处理大数据对象clob数据和blob数据
直接上下代码:
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 Administrator 15 * 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 Administrator 18 * 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 }