⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writeandreadblob.java

📁 java swing源码 欢迎下载 有问题请联系 我一定负责到底
💻 JAVA
字号:
package com.sinosoft.common;

import java.io.*;
import java.sql.*;
import java.util.*;

import oracle.sql.*;

public class WriteAndReadBLOB{
	public Connection conn = null;
	public PreparedStatement pstmt = null;
	public BLOB bb = null;
	public ResultSet rs = null;
	public String sql = "";
	
	public WriteAndReadBLOB(){
		try {
			conn = DataConPool.getConnection();
		} catch (Exception e) {
			System.out.println("WriteAndReadBLOB:init error!" + e.toString());
		}
	}
	
	/**更新BLOB字段,一次只能更新一条记录,如果所影响的记录为多条,则不执行更新,返回false
	 * @param table      表名
	 * @param field		 需要更新的字段名
	 * @param key		 查询字段名,最好为主键或者Unique的字段
	 * @param keyValue	 查询字段值
	 * @param in	  	 输入流
	**/
	private boolean updateBLOB(String table,String field,String key,String keyValue,BufferedInputStream in){
		boolean defaultcommit = true;
		try{
			defaultcommit = conn.getAutoCommit();//保存原来数据库提交设置
			conn.setAutoCommit(false);
		}catch(Exception e){
			System.out.println("WriteAndReadBLOB:updateBLOB() "+e.getMessage());
			return false;
		}
		try{
			sql = "UPDATE " + table + " set " + field + "=EMPTY_BLOB() where " + key + "=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,keyValue);
			pstmt.executeUpdate();                //先把BLOB字段置空
			sql = "SELECT " + field + " from " + table + " where " + key + "=? for update";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,keyValue);
			rs = pstmt.executeQuery();
			if(!rs.next()){
				System.out.println("没有查询到符合条件的记录");
				return false;
			}
			bb = (BLOB) rs.getBlob(1);     //获得BLOB的cursor
			if(rs.next()){
				System.out.println("存在多条符合条件的记录");
				return false;
			}
			OutputStream out = bb.getBinaryOutputStream();
			byte[] data = new byte[bb.getBufferSize()];  
			int count = -1;
			while ((count = in.read(data)) != -1) {
				out.write(data, 0, count);
			}
			in.close();
			in = null;
			out.close();
			out = null;
			sql = "UPDATE " + table + " set " + field + "=? where " + key + "=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setBlob(1,bb);
			pstmt.setString(2,keyValue); 
			pstmt.executeUpdate();
			pstmt.close();
			conn.commit();
			rs.close();
			rs = null;
			conn.setAutoCommit(defaultcommit);
		}catch(Exception e){
			try{
				conn.rollback();
			}catch(Exception ex){
				System.out.println("WriteAndReadBLOB:updateBLOB():conn.rollback() "+ex.getMessage());
			}
			System.out.println("WriteAndReadBLOB:updateBLOB() "+e.getMessage());
			return false;
		}finally{
			try{
				conn.close();
			}catch(Exception e){
				System.out.println("WriteAndReadBLOB:updateBLOB():conn.close() "+e.getMessage());
			}
		}
		return true;
	}
	
	/**读取BLOB字段,记录可能为多条,返回List
	 * @param table      表名
	 * @param field		 需要读取的字段名
	 * @param key		 查询字段名
	 * @param keyValue	 查询字段值
	**/
	private List readBLOB(String table,String field,String key,String keyValue){
		List l = new ArrayList();
		try{
			sql = "SELECT " + field + " from " + table + " where " + key + "=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,keyValue);
			rs = pstmt.executeQuery();
			while(rs.next()){
				bb = (BLOB)rs.getObject(field);
				l.add(bb.getBinaryStream());
			}
		}catch(Exception e){
			System.out.println("WriteAndReadBLOB:readBLOB "+e.getMessage());
		}
		return l;
	}
	
	/**更新内容为字符串的BLOB字段,一次只能更新一条记录,如果所影响的记录为多条,则不执行更新,返回false
	 * @param table      表名
	 * @param field		 需要更新的字段名
	 * @param key		 查询字段名,最好为主键或者Unique的字段
	 * @param keyValue	 查询字段值
	 * @param content	 更新的字符串内容
	**/
	public boolean writeString(String table,String field,String key,String keyValue,String content){
		BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(content.getBytes()));
		return this.updateBLOB(table, field, key, keyValue, in);
	}
	
	/**更新内容为文件的BLOB字段,一次只能更新一条记录,如果所影响的记录为多条,则不执行更新,返回false
	 * @param table      表名
	 * @param field		 需要更新的字段名
	 * @param key		 查询字段名,最好为主键或者Unique的字段
	 * @param keyValue	 查询字段值
	 * @param file		 更新的文件路径
	**/
	public boolean writeFile(String table,String field,String key,String keyValue,String file){
		BufferedInputStream in = null;
		try{
			in = new BufferedInputStream(new FileInputStream(file));
		}catch(Exception e){
			System.out.println("WriteAndReadBLOB.writeFile Error " + e.getMessage());
			return false;
		}
		return this.updateBLOB(table, field, key, keyValue, in);
	}
	
	/**读取内容为字符串的BLOB字段,记录可能为多条,返回String[]
	 * @param table      表名
	 * @param field		 需要读取的字段名
	 * @param key		 查询字段名
	 * @param keyValue	 查询字段值
	**/
	public String[] readString(String table,String field,String key,String keyValue){
		List l = this.readBLOB(table, field, key, keyValue);
		int i = 0;
		int total = l.size();
		String[] result = new String[total];
		while(l!=null && i<total){
			InputStream in = (InputStream)l.get(i);
			ByteArrayOutputStream  out = new ByteArrayOutputStream();
			i++;
			byte[] b = new byte[1024];
			StringBuffer str = new StringBuffer();
			int count = -1;
			try{
				while((count=in.read(b))!=-1){
					out.write(b, 0, count);
				}
				str = new StringBuffer(out.toString());
				if(str.length()!=0)result[i-1] = str.toString();
				in.close();
				in = null;
			}catch(Exception e){
				System.out.println("WriteAndReadBLOB.readString Error " + e.getMessage());
			}
		}
		return result;
	}
	
	/**读取内容为文件的BLOB字段,记录可能为多条,无返回,读取的文件保存在指定的位置
	 * @param table      表名
	 * @param field		 需要读取的字段名
	 * @param key		 查询字段名
	 * @param keyValue	 查询字段值
	 * @param file       文件存放的路径,比如"e:/test.txt"
	**/
	public void readFile(String table,String field,String key,String keyValue,String file){
		int lastIndex = file.lastIndexOf(".");
		String filePrefix = "";
		String filePostfix = "";
		if(lastIndex!=-1){
			filePrefix = file.substring(0,lastIndex);
			filePostfix = "." + file.substring(lastIndex+1);
		}else filePrefix = file;
		List l = this.readBLOB(table, field, key, keyValue);
		int i = 0;
		int total = l.size();
		while(l!=null && i<total){
			String tmpPath = "";
			if(i==0)tmpPath = file;
			else tmpPath = filePrefix + i + filePostfix;
			InputStream in = (InputStream)l.get(i);
			i++;
			byte[] b = new byte[1024];
			int count = -1;
			try{
				FileOutputStream out = new FileOutputStream(tmpPath);
				while((count=in.read(b))!=-1){
					out.write(b, 0, count);
				}
				in.close();
				out.close();
				in = null;
				out = null;
			}catch(Exception e){
				System.out.println("WriteAndReadBLOB.readFile Error " + e.getMessage());
			}
		}
	}
	
	public static void main(String[] args){
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -