读取数据库中BLOB字段

@Override
	public String getFormViewById(final String id) {
		return (String)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				String sql = "select formView from sys_forms where id='" + id + "'";
				Connection conn = null;
				PreparedStatement stmt = null;
				ResultSet rs = null;
				String content = "";
				try {
					conn = session.connection();
					stmt = conn.prepareStatement(sql);
					rs = stmt.executeQuery();
					while(rs.next()){
						Blob blob = (Blob) rs.getBlob("formView");  
						int bolblen = (int) blob.length();  
						byte[] data = blob.getBytes(1, bolblen);  
						content = new String(data,"utf-8");
					}
				}finally{
					conn.close();
					stmt.close();
				}
				
				return content;
			}
		});
	} 
  

你可能感兴趣的:(【javaSE】)