package com.sinosoft.am.org.jdbcUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import com.sinosoft.cm.DBUtils; import com.sinosoft.cm.ex.SQLStringException; public class ORGTemplate { private static final Logger log = Logger.getLogger(ORGTemplate.class); public ORGTemplate() { super(); } /** * * @param sql PreparedStatement sql语句 * @param args 占位符参数 * @return 1 更新成功 0 跟新失败 * @throws SQLException */ public int update(String sql,Object...args) throws SQLException{ Connection conn = null; PreparedStatement ps = null; try { conn = DBUtils.getConnection(); ps = conn.prepareStatement(sql); if(args!= null){ for(int i =0; i> query(String sql,Object...args) throws SQLException{ ArrayList> al = new ArrayList>(); String[] fields = null; Connection conn = null; PreparedStatement ps = null; ResultSet rs=null; try { conn = DBUtils.getConnection(); ps = conn.prepareStatement(sql); if(args!=null){ for(int i = 0;i dataMap = new HashMap(); for (int i = 0; i < fields.length; i++) { dataMap.put(fields[i], rs.getString(fields[i])); } al.add(dataMap); } } } catch (SQLException e) { e.printStackTrace(); throw new SQLStringException(this.getClass()+":query(String sql,Object...args);sql语句异常"); }finally{ DBUtils.close(rs, ps, conn); } return al; } public ArrayList> getSerachResult(String sql,String...args) throws SQLException{ ArrayList> al = new ArrayList>(); String[] fields = null; Connection conn = null; PreparedStatement ps = null; ResultSet rs=null; ArrayList parList = new ArrayList(); try { conn = DBUtils.getConnection(); ps = conn.prepareStatement(sql); if(args!=null){ for(int i = 0;i dataMap = new HashMap(); for (int i = 0; i < fields.length; i++) { dataMap.put(fields[i], rs.getString(fields[i])); } al.add(dataMap); } } } catch (SQLException e) { e.printStackTrace(); throw new SQLStringException(this.getClass()+":query(String sql,Object...args);sql语句异常"); }finally{ DBUtils.close(rs, ps, conn); } return al; } public List getResultList(String sql,String...args) throws SQLStringException{ String[] fields = null; Connection conn = null; PreparedStatement ps = null; ResultSet rs=null; ArrayList list = new ArrayList(); ArrayList parList = new ArrayList(); try { conn = DBUtils.getConnection(); ps = conn.prepareStatement(sql); if(args!=null&&args.length>0){ for(int i=0;i getSearchResult(String sql,String...args) throws SQLException{ String[] fields = null; Connection conn = null; PreparedStatement ps = null; ResultSet rs=null; ArrayList list = new ArrayList(); ArrayList parList = new ArrayList(); try { conn = DBUtils.getConnection(); ps = conn.prepareStatement(sql); if(args!=null&&args.length>0){ for(int i=0;i> list){ StringBuffer sb = new StringBuffer(); for(Map map :list){ sb.append("{"); for (Map.Entry entry : map.entrySet()) { sb.append("\""+entry.getKey()+"\":\""+entry.getValue()+"\","); } if(sb.lastIndexOf(",")>-1){ sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append("},"); } if(sb.lastIndexOf(",")>-1){ sb.deleteCharAt(sb.lastIndexOf(",")); } return sb.toString(); } public String mapToJson(Map map){ StringBuffer sb = new StringBuffer(); for(Map.Entry m:map.entrySet()){ sb.append("{"); sb.append("\""+m.getKey()+"\":\""+m.getValue()+"\","); if(sb.lastIndexOf(",")>-1){ sb.deleteCharAt(sb.lastIndexOf(",")); } sb.append("},"); } if(sb.lastIndexOf(",")>-1){ sb.deleteCharAt(sb.lastIndexOf(",")); } return sb.toString(); } /** * * 给list中的map 添加属性 key value 根据的map中的key 对应的value值 * @return *//* public List> addExtParamToList(List> list,String key,String newKey){ for(Map map : list){ String keyValue = map.get(key); String pinyin = Pinyin4jUtil.getPinYin(src); map.put(newKey, keyValue); } return null; }*/ public static void main(String[] args){ StringBuffer sql=new StringBuffer(); String id = "8F6F2D6B24494CB6B1F1E0931E7AF87D"; ORGTemplate org = new ORGTemplate(); List> list = new ArrayList>(); try { /*sql.append("--查询给定id及其一级子机构的信息 \n"); sql.append("SELECT A.ORG_ID,A.ORG_NAME,A.PARENT_ID,A.ORG_LEVEL,A.IS_VIRTUAL, \n"); sql.append("DECODE((SELECT COUNT(1) FROM EMC_DEPARTMENT_IN B WHERE B.PARENT_ID = A.ORG_ID),'0','0','1') AS 是否有子机构 \n"); sql.append("FROM EMC_DEPARTMENT_IN A WHERE PARENT_ID = ? OR ORG_ID= ? AND IS_DEL='0'\n");*/ /*sql.append("--查询给定id及其一级子机构的信息 \n"); sql.append("WITH P AS(SELECT U.USER_ID \"id\",U.REAL_NAME \"name\",U.ORG_ID \"parentId\",'/nwyj/scripts/qui/libs/icons/home.png' \"icon\",U.POSITION \"orgLevel\", U.MOBILE \"perPhone\",U.GENDER \"isParent\",'per' \"leafType\" \n"); sql.append("FROM SYS_USER_INFO U ,SYS_DEPARTMENT D,SYS_DEPT_USER DU \n"); sql.append("WHERE U.USER_ID = DU.USER_ID AND D.DEPT_ID = DU.DEPT_ID AND D.DEPT_ID=? \n"); sql.append("AND U.USER_ID NOT IN (SELECT INLINK_INDEX FROM EMC_AM_ORG_INLINK X \n"); sql.append("WHERE X.VIRTUAL_ORG_ID =? AND X.IS_DEL ='0') ) , \n"); sql.append("O AS(SELECT A.DEPT_ID ,A.DEPT_NAME ,A.PARENT_ID ,'/nwyj/scripts/qui/libs/icons/user-black.png' icon,A.DEPT_LEVEL,A.CORP_ID e, \n"); sql.append("DECODE(DECODE((SELECT COUNT(1) FROM SYS_DEPARTMENT B WHERE B.PARENT_ID = A.DEPT_ID),0,0,1)+ \n"); sql.append("DECODE((SELECT COUNT(1) FROM SYS_USER_INFO U ,SYS_DEPARTMENT D,SYS_DEPT_USER DU \n"); sql.append("WHERE U.USER_ID = DU.USER_ID AND D.DEPT_ID = DU.DEPT_ID AND D.DEPT_ID=A.DEPT_ID \n"); sql.append("AND U.USER_ID NOT IN (SELECT INLINK_INDEX FROM EMC_AM_ORG_INLINK X \n"); sql.append("WHERE X.VIRTUAL_ORG_ID =? AND X.IS_DEL ='0') \n"); sql.append("),0,0,1),0,'false','true') AS isParent,'org' leafType \n"); sql.append("FROM SYS_DEPARTMENT A WHERE (A.PARENT_ID = ? OR A.DEPT_ID=?) AND A.IS_DEL='0') \n"); sql.append("SELECT * FROM P \n"); sql.append("UNION ALL \n"); sql.append("SELECT * FROM O \n");*/ sql.append("--查询给定id一级子机构的信息(不包括他本身) \n"); sql.append("WITH T AS (SELECT * FROM SYS_DEPARTMENT WHERE INSTR(DEPT_PATH,'_'||?||'_')>0) \n"); sql.append("SELECT A.DEPT_ID \"id\",A.DEPT_NAME \"name\",A.PARENT_ID \"parentId\",A.DEPT_LEVEL \"orgLevel\", \n"); sql.append("DECODE((SELECT COUNT(1) FROM T B WHERE B.PARENT_ID = A.DEPT_ID),0,'false','true') AS \"isParent\" \n"); sql.append("FROM T A WHERE A.PARENT_ID = ? OR A.DEPT_ID =? \n "); list = org.query(sql.toString(),id,id,id); System.out.println(org.listToString(list)); } catch (SQLException e) { e.printStackTrace(); } Map map = new HashMap(); map.put("1", "2"); map.put("2", "3"); map.put("3", "4"); String str = new ORGTemplate().mapToJson(map); System.out.println(str); } }