spring jdbc 获取 clob

spring jdbc 获取 clob:
String sql = "select * from user where rownum = 1";
logger.debug("sql: " + sql);

final LobHandler lobHandler = new OracleLobHandler();
List userList = jdbcTemplateAsd.query(sql,
          new RowMapper() {
            public Object mapRow(ResultSet rs, int i) throws SQLException {
              Map results = new HashMap();
              String userProfile = lobHandler.getClobAsString(rs, "USER_PROFILE");
              logger.debug("userProfile:" + userProfile);
              results.put("userProfile", userProfile);
              return results;
            }
          });

String jsonString = (String) ((Map) userList.get(0)).get("userProfile");
User user = JSONObject.parseObject(jsonString, User.class);
logger.debug("user:" + user);

ResultSet 获取 clob: private void clobExport() throws ClassNotFoundException, SQLException,
            IOException {
        CLOB clob = null;
        String sql = "select * from USER where lower(CLIENT_ID) = '[email protected]' order by START_TIME desc";
        DriverManager.registerDriver(new OracleDriver());
        Connection conn = DriverManager.getConnection(url, user, pwd);
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        String content = "";
        if (rs.next()) {
            clob = (oracle.sql.CLOB) rs.getClob("USER_PROFILE");
            content = ClobToString(clob);
        }
        stmt.close();
        conn.close();
        System.out.println(content);
    }

    public String ClobToString(CLOB clob) throws SQLException, IOException {
        String reString = "";
        Reader is = clob.getCharacterStream();
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        return reString;
    }

    public static void main(String[] args) throws IOException,
            ClassNotFoundException, SQLException {
        ClobTest clobtest = new ClobTest();
         clobtest.clobExport();
    }

apache ToStringBuilder:public String toString(){
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}

你可能感兴趣的:(Spring JDBC)