package com.planarChart.common; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.apache.log4j.Logger; import com.formaction.vo.RowSet; import com.persistence.service.JDBCHelper; import com.persistence.service.logic.LogicFactory; import com.persistence.service.logic.LogicHelper; import com.sysmodel.datamodel.xmlmodel.ModelFactory; import com.sysmodel.datamodel.xmlmodel.able.MdpClass; import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource; import com.sysmodel.datamodel.xmlmodel.able.SysModel; public class ChartJDBCHelper extends JDBCHelper{ private final static Logger log = Logger.getLogger(ChartJDBCHelper.class); private Connection connection = null; private SysModel sysmodel = ModelFactory.getSysmodel(); private LogicHelper helper = null; private int classId; public ChartJDBCHelper(int classId) { this.classId = classId; } public ChartJDBCHelper() { this.classId = 99; } /** * 根据提供的SQL语句执行查询,并返回结果 * * @param sql * @return */ public RowSet queryData(String sql) { PreparedStatement ps = null; ResultSet rs = null; RowSet res = new RowSet(); try { MdpClass mdpClass = sysmodel.getMdpClassByClassID(this.classId); MdpDataSource dataSource = sysmodel.getDataSourceByCode(mdpClass.getDataSource()); helper = LogicFactory.getInstance(dataSource.getDataBase()); connection = helper.getConnection(dataSource); ps = connection.prepareStatement(sql); rs = ps.executeQuery(); int columNumber = rs.getMetaData().getColumnCount();// 记录结果集中的列数量 Map map = null; String[] columName = new String[columNumber]; for (int m = 0; m < columNumber; m++) { columName[m] = rs.getMetaData().getColumnName(m + 1).toUpperCase(); } while (rs.next()) { map = new HashMap(); for (int n = 0; n < columNumber; n++) { String key = columName[n]; String value = rs.getString(n + 1); map.put(key, value); } res.getRows().add(map); } } catch (Exception e) { e.printStackTrace(); log.error(e); } finally { if (rs != null) try { rs.close(); } catch (SQLException e1) { log.error(e1.getStackTrace().toString()); } if (ps != null) try { ps.close(); } catch (SQLException e1) { log.error(e1.getStackTrace().toString()); } if (connection != null) try { connection.close(); } catch (SQLException e) { log.error(e.getStackTrace().toString()); e.printStackTrace(); } } return res; } }