?
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 ""; } }
?