JDBC处理包含CLOB字段_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JDBC处理包含CLOB字段

JDBC处理包含CLOB字段

 2015/1/14 15:29:49  小鱼不爱水  程序员俱乐部  我要评论(0)
  • 摘要:publicstaticList<Map<String,Object>>getDetail(finalStringinstance,finalStringsql)throwsException{Stringkey="";Connectioncon=null;Statementst=null;ResultSetrs=null;ResultSetMetaDatarsmd=null;List<Map<String,Object>>
  • 标签:

?

class="java" name="code">public static List<Map<String,Object>> getDetail(final String instance,final String sql) throws Exception{
 
    String key="";
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;
    ResultSetMetaData rsmd=null;
    List<Map<String,Object>> resultList =new ArrayList<Map<String,Object>>();
     
    Map<String,String> jdbcMap=new HashMap<String,String>();
 
    //jdbcMap=praseXml(instance);//取得jdbc连接串
 
    try {
        Class.forName(jdbcMap.get("driver"));
        con=DriverManager.getConnection(jdbcMap.get("url"),DesEncryptUtils.decrypt(jdbcMap.get("name")),DesEncryptUtils.decrypt(jdbcMap.get("pass")));
        st = con.createStatement();
        rs = st.executeQuery(sql);
        rsmd=rs.getMetaData();
        logger.info("...Running...");
        int columnCount = rsmd.getColumnCount();
        Map<String, Object> map = null;
         
        while (rs.next()) {
            map = new HashMap<String, Object>();
            for (int i = 1; i <= columnCount; i++) {
                key = rsmd.getColumnName(i);
                if( rs.getObject(key) instanceof Clob){
                    Clob clob = rs.getClob(key);// java.sql.Clob类型 
                    String clobValue=getClobString(clob);
                    map.put(key,clobValue);
                }else{
                    Object value = rs.getObject(key);
                    map.put(key, value);
                }
            }
            resultList.add(map);
        }
         
    }finally{
    con.close();
        st.close();
        rs.close();
    }
 
    return resultList;
}

?

?

//处理CLOB主要方法   
public static String getClobString(Clob c) {
        try {
            Reader reader = c.getCharacterStream();
            if (reader == null) {
                return null;
            }
            StringBuffer sb = new StringBuffer();
            char[] charbuf = new char[4096];
            for (int i = reader.read(charbuf); i > 0; i = reader.read(charbuf)) {
                sb.append(charbuf, 0, i);
            }
            return new String(sb.toString().getBytes("UTF-8"));
        } catch (Exception e) {
            return "";
        }
    }

?

  • 相关文章
发表评论
用户名: 匿名