JDBC操作大数据CLOB和BLOB

12 篇文章 0 订阅

JDBC操作大数据CLOB和BLOB:

package jdbc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import word.JavaWord;


/**
 * JDBC操作大数据CLOB和BLOB
 * @author wangzg
 *
 */
public class LobTest {
	
	/**
	 * @Title: main
	 * @Description:
	 * @param:
	 * @return void 
	 * @user: wangzg
	 * @Date:2014-7-2
	 * @throws
	 */
	public static void main(String[] args) {
		
		//insertLob("doc\\wzg.doc");
		
		//insertLob2("doc\\wzg.doc");
		
		//queryLob();
	}
	
	/**
	 * 
	 * @Title: getConnection
	 * @Description:
	 * @param:
	 * @return Connection 
	 * @user: wangzg
	 * @Date:2014-7-4
	 * @throws
	 */
	public static Connection getConnection() throws ClassNotFoundException, SQLException{
		Connection conn = null;
		//加载oracle驱动
		Class.forName("oracle.jdbc.driver.OracleDriver");
		//连接数据库
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
		String user ="wzg";
		String password = "wzg";
		conn = DriverManager.getConnection(url, user, password);
		return conn;
	}

	/**
	 * setBinaryStream
	 * @Title: insertLob
	 * @Description:
	 * @param:
	 * @return void 
	 * @user: wangzg
	 * @Date:2014-7-4
	 * @throws
	 */
	public static void insertLob(String filePath){

		System.out.println("insert start ................");
		Connection conn = null;
		java.sql.PreparedStatement pstm = null;
		ResultSet rs = null;
		
		File file = new File(filePath);
		
		String fileText = JavaWord.readWord2003(filePath);
		String fileName = file.getName();
		int fileLength = (int) file.length();
		
		System.out.println(fileName);
		
		try {
			InputStream isFileBlob = new FileInputStream(file);
			conn = getConnection();
			pstm = conn.prepareStatement("insert into t_file(id,name,context,bfile)values(TEST_SEQUENCE.NEXTVAL,?,?,?)");
			pstm.setString(1, fileName);
			pstm.setString(2, fileText);
			
			//fileLength 必须为int类型
			pstm.setBinaryStream(3, isFileBlob,fileLength);
			
			pstm.executeUpdate();
			
			pstm.close();
			isFileBlob.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		System.out.println("insert end ................");
	}	
	
	/**
	 * for update
	 * @Title: insertLob2
	 * @Description:
	 * @param:
	 * @return void 
	 * @user: wangzg
	 * @Date:2014-7-4
	 * @throws
	 */
	public static void insertLob2(String filePath){
		 
		
		System.out.println("insert2 start ................");
		Connection conn = null;
		java.sql.PreparedStatement pstm = null;
		ResultSet rs = null;
		
		File file = new File(filePath);
		
		String fileText = JavaWord.readWord2003(filePath);
		String fileName = file.getName();
		int fileLength = (int) file.length();
		
		System.out.println(fileName);
		
		try{
			 
			 conn = getConnection();
			 conn.setAutoCommit(false);
			 
			 pstm = conn.prepareStatement("select TEST_SEQUENCE.NEXTVAL from dual");
			 rs = pstm.executeQuery();
			 int index = 0;
			 if(rs.next()){
				  index  = rs.getInt(1);
			 }
			 
			 System.out.println(index);
			 
			 pstm = conn.prepareStatement("insert into t_file(id,name,context,bfile)values(?,?,?,empty_blob())");
			 pstm.setInt(1, index);
			 pstm.setString(2, fileName);
			 pstm.setString(3, fileText);
			 
			 pstm.executeUpdate();
			 
			 pstm = conn.prepareStatement("select bfile from  t_file  where id= ?  for update");
			 pstm.setInt(1, index);
			 
			 rs = pstm.executeQuery();  
			 if (rs.next()) {  
					 Blob blob=rs.getBlob(1);  
					 //到数据库的输出流  
					 OutputStream outStream = blob.setBinaryStream(0); 
					 byte[] bu=new byte[1024];
					 int num;
					 InputStream fis = new FileInputStream(file);
					 while((num=fis.read(bu))>0){
						 outStream.write(bu,0,num);
					 }
					 outStream.flush();  
					 outStream.close(); 
					 fis.close();
			 }
			 
			 conn.commit();
			 conn.setAutoCommit(true);
			 
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		System.out.println("insert2 end ................");
	}
	
	/**
	 * 
	 * @Title: queryLob
	 * @Description:
	 * @param:
	 * @return void 
	 * @user: wangzg
	 * @Date:2014-7-4
	 * @throws
	 */
	public static void queryLob(){
		
		System.out.println("query start ................");
		Connection conn = null;
		java.sql.PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			pstm = conn.prepareStatement("select * from t_file order by id asc");
			rs = pstm.executeQuery();
			
			while(rs.next()){
				
				String id = rs.getString(1);
				System.out.println(id);
				String name = rs.getString(2);
				System.out.println(name);
				
				java.sql.Blob bFile = rs.getBlob(4); 
				InputStream bis = bFile.getBinaryStream() ;
				OutputStream os = new FileOutputStream("doc\\"+getFileName(name)+id+getFileType(name));
				int b = bis.read(); 
				while (b != -1 ) {
					os.write((char)b);
					b = bis.read();
				}
				os.flush();
				os.close();
				bis.close();
				
			}
			rs.close();
			pstm.close();
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("query end ................");
	}
    
	/**
	 * 
	 * @Title: getFileName
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-4
	 * @throws
	 */
	public static String getFileName(String fileName){
		return fileName.substring(0, fileName.lastIndexOf('.'));
	}
	
	/**
	 * 
	 * @Title: getFileType
	 * @Description:
	 * @param:
	 * @return String 
	 * @user: wangzg
	 * @Date:2014-7-4
	 * @throws
	 */
	public static String getFileType(String fileName){
		return fileName.substring(fileName.lastIndexOf('.'));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值