package UIB.JHY; import CoreFS.SA01.CoreIComponent; import CoreFS.SA06.CoreReturnObject; import UIB.COM.XmlSqlParsersFactory; import java.sql.SQLException; import java.util.ArrayList; public class ComQuery extends CoreIComponent { /** * @param list 第一个参数为SQL的ID * @return * @throws Exception * @desc 简单查询,自动判断是否带有参数传递 */ public CoreReturnObject doSimpleQuery(String dao, ArrayList list) throws Exception { CoreReturnObject cro = null; if (list == null) throw new Exception("错误,参数不允许为null!"); if (list.size() == 0) throw new Exception("错误,没有传入任何参数!"); String sqlID = list.get(0).toString();//传入的第一个参数为SQL的ID try { if (list.size() == 1)//无参查询 { cro = this.queryNoParameter(dao, sqlID); } else//带参查询 { cro = this.queryWithParameter(dao, sqlID, list); } } catch (Exception ex) { System.out.print("查询参数错误: " + ex.getMessage()); } return cro; } /** * @param * @return * @desc 连续查询,执行多条查询语句,一次返回结果 */ public CoreReturnObject doContQluery(String dao, ArrayList list) throws Exception { if (list == null) throw new Exception("错误,参数不允许为null!"); if (list.size() == 0) throw new Exception("错误,没有传入任何参数!"); ArrayList al = new ArrayList(); CoreReturnObject cro1 = new CoreReturnObject();//最终结果 for (int i = 0; i < list.size(); i++) { al.add(doSimpleQuery(dao, (ArrayList) list.get(i)).getResult()); } cro1.setResult(al); return cro1; } /** * @param sqlID * @param list * @return * @throws SQLException * @desc 带参数SQL */ private CoreReturnObject queryWithParameter(String dao, String sqlID, ArrayList list) throws SQLException { CoreReturnObject cro = null; int len = list.size() - 1; Object[] obj = new Object[len]; //System.out.println("list长度:"+list.size()); for (int i = 0; i < len; i++) { obj[i] = list.get(i + 1).toString();//第0个参数为SQL语句,SQL语句中的参数需要从第一个开始 // System.out.println("参数值:"+obj[i].toString()); } cro = this.getDao(dao).ExcuteQuery(XmlSqlParsersFactory.getSql(sqlID), obj); return cro; } /** * @param sqlID * @return * @throws SQLException * @desc 无参数查询 */ private CoreReturnObject queryNoParameter(String dao, String sqlID) throws SQLException { CoreReturnObject cro = null; cro = this.getDao(dao).ExcuteQuery(XmlSqlParsersFactory.getSql(sqlID)); return cro; } }