JAVA中操作clob与blob大字段属性

      相信很多朋友在oracle数据入库的时候经常会遇到大字段处理的问题,在经过多次的处理这个问题之后小编逐渐进行了总结,好啦不废话啦直接进击主题。(此处的数据库连接,朋友们可以去搜集其他资料,此处只是集中针对大字段的存储更新问题)。

    其中blob的jar包是oracle.sql.BLOB,大家自行在网上搜索下载即可。

一、blob数据的处理

    字段属性变动前都会使用empty_blob()进行该属性的初始化,之后再使用流的方式将需要变动的数据写入数据库。

1、插入方法

/*
	 * pidColumnName - 主键对应的列名
	 * pidColumnValue - 主键值
	 * blobColumnName - blob类型的列名
	 * blobColumnValue - blob的值
	 * tableName - 数据表的表名
	 * */
	public boolean insertSqlByPid(Map param) {
		String pidColumnName = param.get("pidColumnName").toUpperCase();
		String pidColumnValue = param.get("pidColumnValue");
		String blobColumnName = param.get("blobColumnName").toUpperCase();
		String blobColumnValue = param.get("blobColumnValue");
		String tableName = param.get("tableName").toUpperCase();
                Connection con;
		try {
                         con = DriverManager.getConnection(url, username,   password);
                         con.setAutoCommit(false);  
     String sql1 = "insert into "+tableName+"("+pidColumnName+","+blobColumnName+") values('"+pidColumnValue+"',empty_blob())";  
	                Statement statement = con.createStatement();  
	                boolean b2 = statement.execute(sql1);  
	  String sql2 = "select "+blobColumnName+" from "+tableName+" where "+pidColumnName+"='"+pidColumnValue+"' for update";  
	                PreparedStatement stmt = con.prepareStatement(sql2);  
	                ResultSet rs = stmt.executeQuery();  
	                OutputStream outStream = null;  
	                if (rs.next()) {  
	                    BLOB blob = (BLOB) rs.getBlob(1);  
	                    outStream = blob.getBinaryOutputStream();  
	                    outStream.write(blobColumnValue.getBytes(), 0,blobColumnValue.getBytes().length);  
	        }  
	        outStream.flush();  
	        outStream.close();  
	        con.commit();  
	        con.close();  
	        return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			  return false;
		}  

	}

2、更新方法

/*
	 * pidColumnName - 主键对应的列名
	 * pidColumnValue - 主键值
	 * blobColumnName - blob类型的列名
	 * blobColumnValue - blob的值
	 * tableName - 数据表的表名
	 * */
	public boolean updateSqlByPid(Map param) {
		String pidColumnName = param.get("pidColumnName").toUpperCase();
		String pidColumnValue = param.get("pidColumnValue");
		String blobColumnName = param.get("blobColumnName").toUpperCase();
		try {
			blobColumnName = new String(blobColumnName.getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String blobColumnValue = param.get("blobColumnValue");
		String tableName = param.get("tableName").toUpperCase();
        Connection con;
		try {
			con = DriverManager.getConnection(url, username,  password);
			con.setAutoCommit(false);  
			String sql1 = "update "+tableName+" set "+blobColumnName+" = empty_blob() where "+pidColumnName+" = '"+pidColumnValue+"'";  
	                Statement statement = con.createStatement();  
	                boolean b1 = statement.execute(sql1);  
	     String sql2 = "select "+blobColumnName +" as BLOB_NAME from "+tableName+" where "+pidColumnName+"='"+pidColumnValue+"' for update";  
	                PreparedStatement stmt = con.prepareStatement(sql2);  
	                ResultSet rs = stmt.executeQuery();  
	                OutputStream outStream = null;  
	                if (rs.next()) {  
	                    BLOB blob = (BLOB) rs.getBlob("BLOB_NAME"); 
	                    outStream = blob.getBinaryOutputStream();  
	                   outStream.write(blobColumnValue.getBytes("UTF-8"), 0,blobColumnValue.getBytes("UTF-8").length); 
	        }  
	        outStream.flush();  
	        outStream.close();  
	        con.commit();  
	        con.close();  
	        return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}  

	}

3、查询方法

/*
	 * pidColumnName - 主键对应的列名
	 * pidColumnValue - 主键值
	 * blobColumnName - blob类型的列名
	 * blobColumnValue - blob的值
	 * tableName - 数据表的表名
	 * */
	public String selectSqlByPid(Map param) {
		String blobInfo = null;
		String pidColumnName = param.get("pidColumnName").toUpperCase();
		String pidColumnValue = param.get("pidColumnValue");
		String blobColumnName = param.get("blobColumnName").toUpperCase();
		String tableName = param.get("tableName").toUpperCase();
                Connection con;
		try {
			con = DriverManager.getConnection(url, username,  password);
			con.setAutoCommit(false);  
	  String sql3 = "select "+blobColumnName+" from "+tableName+" where "+pidColumnName+"='"+pidColumnValue+"' ";  
	                PreparedStatement stmt2 = con.prepareStatement(sql3);  
	                ResultSet rs2 = stmt2.executeQuery();  
	                if (rs2.next()) {  
	                    BLOB blob = (BLOB) rs2.getBlob(1);  
	                    blobInfo = new String(blob.getBytes((long)1, (int)blob.length()),"UTF-8");  
	                }  
	                con.close(); 
	                return blobInfo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return blobInfo;
		}  

	}

二、clob数据的处理

    此处clob 的处理与blob属性的处理是略不相同的,但是原理还是一样的,此处是通过reader流转为byte数组再转为String数据返回

1、查询方法

        

/*
	 * pidColumnName - 主键对应的列名
	 * pidColumnValue - 主键值
	 * clobColumnName - clob类型的列名
	 * clobColumnValue - clob的值
	 * tableName - 数据表的表名
	 * */

	        public String selectSqlByPid(Map param) {
		String clobInfo = null;
		String pidColumnName = param.get("pidColumnName").toUpperCase();
		String pidColumnValue = param.get("pidColumnValue");
		String clobColumnName = param.get("clobColumnName").toUpperCase();
		String tableName = param.get("tableName").toUpperCase();
		Reader inStream =null;
                Connection con;
		try {
			con = DriverManager.getConnection(url, username, password);
			con.setAutoCommit(false);  
	  String sql = "select "+clobColumnName+" from "+tableName+" where "+pidColumnName+"='"+pidColumnValue+"' ";  
	                PreparedStatement stmt = con.prepareStatement(sql);  
	                ResultSet rs = stmt.executeQuery();  
	                if (rs.next()) {  
	                            Clob clob = rs.getClob(1);
	                            if(clob==null){
	            	            System.out.println("照片信息为空");
	                }else{
	            	            //clob解码编译为base64
	            	            String pic = clob.getSubString((long)1, (int) clob.length());
	            	            byte[] binary  = (new sun.misc.BASE64Decoder()).decodeBuffer(pic.replaceAll(" ", ""));
	            	            //照片存到本地
	            	            String path = "D:/zhks/picture/temp/";
	            	            File temp = new File(path);
	            	if(!temp.exists()) {
	            	    	temp.mkdirs(); 
	            	}
	            	String filename = path+"/"+pidColumnValue+".jpg";
	            	FileOutputStream fos = null;
	            	fos = new FileOutputStream(filename);
	            	fos.write(binary, 0, binary.length);
	            	fos.close();
	            	clobInfo = filename;
	            }
	        }  
	        con.commit();
	        con.close(); 
	        return clobInfo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return clobInfo;
		}  

	}

2、更新方法

    

 /*
	 * pidColumnName - 主键对应的列名
	 * pidColumnValue - 主键值
	 * clobColumnName - clob类型的列名
	 * clobColumnValue - clob的值
	 * tableName - 数据表的表名
	 * */

        public boolean updateByPid(Map param) {
		String pidColumnName = param.get("pidColumnName").toUpperCase();
		String pidColumnValue = param.get("pidColumnValue");
		String clobColumnName = param.get("clobColumnName").toUpperCase();
		try {
			clobColumnName = new String(clobColumnName.getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String clobColumnValue = param.get("clobColumnValue");
		String tableName = param.get("tableName").toUpperCase();
        Connection con;
		try {
			con = DriverManager.getConnection(url, username, password);
			con.setAutoCommit(false);  
			String sql1 = "update "+tableName+" set "+clobColumnName+" = empty_clob() where "+pidColumnName+" = '"+pidColumnValue+"'";  
	                Statement statement = con.createStatement();  
	                boolean b1 = statement.execute(sql1);  
	     String sql2 = "select "+clobColumnName +" as CLOB_NAME from "+tableName+" where "+pidColumnName+"='"+pidColumnValue+"' for update";  
	                PreparedStatement stmt = con.prepareStatement(sql2);  
	                ResultSet rs = stmt.executeQuery();  
	                if (rs.next()) {
	        	    oracle.sql.CLOB clob =  (oracle.sql.CLOB) rs.getClob("BLOB_NAME"); 
	        	    Writer outStream = clob.getCharacterOutputStream();  
	        	    char[] c = clobColumnValue.toCharArray();  
	        	    outStream.write(c, 0, c.length);
	        	    outStream.flush();  
	                    outStream.close(); 
	            }  
	            con.commit();  
	            con.close();  
	            return true;
		    } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		    }  
	    }

你可能感兴趣的:(编程问题处理)