根据条件执行查询

时间:2016-12-07 10:49:55

标签: java mysql

PreparedStatement pstmt = null;
    ResultSet rs = null;

    List<SelectItemsDTO> listItem = new ArrayList<SelectItemsDTO>();
    try {
        StringBuilder query =new StringBuilder();

        query = query.append(SLCMQueryConstant.GET_CollegeChid_LIST);

        pstmt = conn.prepareStatement(query.toString());
        pstmt.setInt(1,commonDto.getOrgID());
        rs = pstmt.executeQuery();
        int pid= 0;
        while (rs.next()) {
        pid= rs.getInt("organization_parent_id");
        }

        if(pid!= 0)
        {
            query = query.append(SLCMQueryConstant.GET_CollegeSub_LIST.toString());

            pstmt = conn.prepareStatement(query.toString());

        }
        else
        {

            query = query.append(SLCMQueryConstant.GET_College_LIST.toString());
            pstmt = conn.prepareStatement(query.toString());

        }


       pstmt.setInt(1,commonDto.getOrgID());
        rs = pstmt.executeQuery();
        while (rs.next()) {
            SelectItemsDTO itemTo = new SelectItemsDTO();

            itemTo.setItemCode(rs.getString("Organization_Name"));
            itemTo.setItemIds(rs.getInt("Organization_ID"));

            listItem.add(itemTo);

        }
    }
public static final String GET_College_LIST= " SELECT Organization_Name,Organization_ID FROM m_organization_master WHERE organization_parent_id = ? ";
public static final String GET_CollegeSub_LIST = " SELECT  Organization_Name,Organization_ID FROM m_organization_master WHERE Organization_ID = ? ";

我想根据if条件执行查询。我从数据库获取PID值然后用作条件。这个代码似乎是一个错误没有为参数1指定值我已经初始化了传递值的参数。

0 个答案:

没有答案