关于sql查询返回json数组格式的案列代码

package com.kingdee.eas.custom.app;

import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import java.sql.SQLException;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK; //import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean; //import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import java.lang.String;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.csinterface.syncdatabase.SQLFacadeFactory;
import com.kingdee.jdbc.rowset.IRowSet;

public class BanktransactionFacadeControllerBean extends
		AbstractBanktransactionFacadeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.app.BanktransactionFacadeControllerBean");

	@Override
	protected String _getBankaccounts(Context ctx) throws BOSException,
			EASBizException {
		String sql = " select   FBANKACCOUNTNUMBER      ,   FNAME_L2   from T_BD_AccountBanks  ";
		IRowSet rowSet = SQLFacadeFactory.getLocalInstance(ctx).getRowset(sql);
		net.sf.json.JSONArray array = new net.sf.json.JSONArray();
		String FBANKACCOUNTNUMBER = "";
		String FNAME_L2 = "";
		try {
			while (rowSet.next()) {
				FBANKACCOUNTNUMBER = rowSet.getString("FBANKACCOUNTNUMBER");
				FNAME_L2 = rowSet.getString("FNAME_L2");
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("BANKACCOUNTNUMBER", FBANKACCOUNTNUMBER);
				jsonObj.put("NAME", FNAME_L2);
				array.add(jsonObj);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return array.toString();
	}

	@Override
	protected String _getBanktransactions(Context ctx, String company,
			String startDate, String endDate) throws BOSException,
			EASBizException {
		String sql2 = "  ";
		if (null != company && !company.equals("")) {
			sql2 = "     and company.FName_l2= '" + company + "'      ";
		}
		String sql = "    select bank.FBankAccountNumber,company.FName_l2, FOPPBANKNUMBER,FOPPUNIT, to_char(FBIZTIME,'yyyy-mm-dd')  "
				+ "  FBIZTIME    ,FDEBITAMOUNT,FCREDITAMOUNT,FTRANSBALANCE from T_BE_TransDetail  detail   inner  join  T_BD_AccountBanks bank on bank.FID=detail.FBankAccountID   inner join T_ORG_Company company on company.fid=detail.FCompanyID "
				+ "where     to_char(FBIZTIME,'yyyy-mm-dd') between   to_char('"
				+ startDate + "') and  to_char('" + endDate + "')   ";
		  StringBuffer stringBuffer = new StringBuffer(sql);
		  stringBuffer.append(sql2);
			IRowSet rowSet = SQLFacadeFactory.getLocalInstance(ctx).getRowset(stringBuffer.toString());
			net.sf.json.JSONArray array = new net.sf.json.JSONArray();
			try {
				while (rowSet.next()) {
					String FBankAccountNumber = rowSet.getString("FBANKACCOUNTNUMBER");
					String FName_l2 = rowSet.getString("FNAME_L2");
					String FOPPUNIT = rowSet.getString("FOPPUNIT");
					String FBIZTIME = rowSet.getString("FBIZTIME");
					double FDEBITAMOUNT = rowSet.getDouble("FDEBITAMOUNT");
					double FCREDITAMOUNT = rowSet.getDouble("FCREDITAMOUNT");
					double FTRANSBALANCE = rowSet.getDouble("FTRANSBALANCE");
					JSONObject jsonObj = new JSONObject();
					jsonObj.put("FBankAccountNumber", FBankAccountNumber);
					jsonObj.put("FName", FName_l2);
					jsonObj.put("FOPPUNIT", FOPPUNIT);
					jsonObj.put("FBIZTIME", FBIZTIME);
					jsonObj.put("FDEBITAMOUNT", FDEBITAMOUNT);
					jsonObj.put("FCREDITAMOUNT", FCREDITAMOUNT);
					jsonObj.put("FTRANSBALANCE", FTRANSBALANCE);
					array.add(jsonObj);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return  array.toString();
	}
}

 

你可能感兴趣的:(BOS项目)