public static String ConvertBLOBtoString(String city_id, String data_date) { String newStr = ""; // 返回字符串 long BlobLength; // BLOB字段长度 byte[] bytes; // BLOB临时存储字节数组 int i = 1; // 循环变量 Connection conn = null; Statement st = null; ResultSet rs = null; try { conn = Conn.getDBConnection(); st = conn.createStatement(); String sql = "select weather from yt_weather where dm_id='" + city_id + "' and to_char(data_date,'yyyy-mm-dd')='" + data_date + "'"; System.out.println(" ConvertBLOBtoString sql" + sql); rs = st.executeQuery(sql); while (rs.next()) { oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("weather"); byte[] msgContent = blob.getBytes(); // BLOB转换为字节数组 BlobLength = blob.length(); // 获取BLOB长度 if (msgContent == null || BlobLength == 0) // 如果为空,返回空值 { return ""; } else { while (i < BlobLength) // 循环处理字符串转换,每次1024;Oracle字符串限制最大4k { bytes = blob.getBytes(i, 1024); i = i + 1024; newStr = newStr + new String(bytes, "GB2312"); } } } // System.out.println(newStr); // System.out.println(newStr.length()); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); rs = null; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (st != null) { try { st.close(); st = null; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { conn.close(); conn = null; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return newStr; }
oracle blob字段转,java oracle blob字段的内容转成字符窜
最新推荐文章于 2024-03-07 10:00:17 发布