ChartJDBCHelper.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.planarChart.common;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.apache.log4j.Logger;
  9. import com.formaction.vo.RowSet;
  10. import com.persistence.service.JDBCHelper;
  11. import com.persistence.service.logic.LogicFactory;
  12. import com.persistence.service.logic.LogicHelper;
  13. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  14. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  15. import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource;
  16. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  17. public class ChartJDBCHelper extends JDBCHelper{
  18. private final static Logger log = Logger.getLogger(ChartJDBCHelper.class);
  19. private Connection connection = null;
  20. private SysModel sysmodel = ModelFactory.getSysmodel();
  21. private LogicHelper helper = null;
  22. private int classId;
  23. public ChartJDBCHelper(int classId) {
  24. this.classId = classId;
  25. }
  26. public ChartJDBCHelper() {
  27. this.classId = 99;
  28. }
  29. /**
  30. * 根据提供的SQL语句执行查询,并返回结果
  31. *
  32. * @param sql
  33. * @return
  34. */
  35. public RowSet queryData(String sql) {
  36. PreparedStatement ps = null;
  37. ResultSet rs = null;
  38. RowSet res = new RowSet();
  39. try {
  40. MdpClass mdpClass = sysmodel.getMdpClassByClassID(this.classId);
  41. MdpDataSource dataSource = sysmodel.getDataSourceByCode(mdpClass.getDataSource());
  42. helper = LogicFactory.getInstance(dataSource.getDataBase());
  43. connection = helper.getConnection(dataSource);
  44. ps = connection.prepareStatement(sql);
  45. rs = ps.executeQuery();
  46. int columNumber = rs.getMetaData().getColumnCount();// 记录结果集中的列数量
  47. Map<String, String> map = null;
  48. String[] columName = new String[columNumber];
  49. for (int m = 0; m < columNumber; m++) {
  50. columName[m] = rs.getMetaData().getColumnName(m + 1).toUpperCase();
  51. }
  52. while (rs.next()) {
  53. map = new HashMap<String, String>();
  54. for (int n = 0; n < columNumber; n++) {
  55. String key = columName[n];
  56. String value = rs.getString(n + 1);
  57. map.put(key, value);
  58. }
  59. res.getRows().add(map);
  60. }
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. log.error(e);
  64. } finally {
  65. if (rs != null)
  66. try {
  67. rs.close();
  68. } catch (SQLException e1) {
  69. log.error(e1.getStackTrace().toString());
  70. }
  71. if (ps != null)
  72. try {
  73. ps.close();
  74. } catch (SQLException e1) {
  75. log.error(e1.getStackTrace().toString());
  76. }
  77. if (connection != null)
  78. try {
  79. connection.close();
  80. } catch (SQLException e) {
  81. log.error(e.getStackTrace().toString());
  82. e.printStackTrace();
  83. }
  84. }
  85. return res;
  86. }
  87. }