78e58ad8eb247fe7cf647d172c6c28ad0c187f89.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package com.sinosoft.cm;
  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import org.apache.log4j.Logger;
  14. import com.sinosoft.cm.chart.util.UUIDUtil;
  15. import com.sinosoft.cm.common.DateUtil;
  16. import com.sinosoft.cm.common.StringUtils;
  17. import com.sinosoft.cm.ex.SQLStringException;
  18. public class CMTemplate {
  19. public static final int SUCCESS=0;
  20. public static final int FAILURE=1;
  21. public static final Boolean autoCommit=true;
  22. public static final Boolean unAutoCommit=false;
  23. private Logger log = Logger.getLogger(this.getClass());
  24. private Connection connection=null;
  25. /**
  26. *無參構造方法
  27. */
  28. public CMTemplate() {
  29. }
  30. /**
  31. * 創建Connection構造方法
  32. * @param conn
  33. */
  34. public CMTemplate(Connection conn){
  35. if(conn==null){
  36. this.connection=DBUtils.getConnection();
  37. }else{
  38. this.connection=conn;
  39. }
  40. }
  41. public int setAutoCommit(Boolean autoCommitState){
  42. int state=CMTemplate.SUCCESS;
  43. try {
  44. if(connection.getAutoCommit())connection.setAutoCommit(autoCommitState);
  45. } catch (SQLException e) {
  46. state=CMTemplate.FAILURE;
  47. log.error(e.getMessage());
  48. }finally{
  49. return state;
  50. }
  51. }
  52. /**
  53. * 事务回滚
  54. * @return
  55. */
  56. public int rollBack(){
  57. if(connection==null)return CMTemplate.FAILURE;
  58. int state=CMTemplate.SUCCESS;
  59. try {
  60. connection.rollback();
  61. } catch (SQLException e) {
  62. state=CMTemplate.FAILURE;
  63. log.error(e.getMessage());
  64. }finally{
  65. return state;
  66. }
  67. }
  68. /**
  69. * 关闭事务链接
  70. * @return
  71. */
  72. public int closeTConnection(){
  73. if(connection==null)return CMTemplate.FAILURE;
  74. int state=CMTemplate.SUCCESS;
  75. try {
  76. connection.commit();
  77. connection.close();
  78. } catch (SQLException e) {
  79. state=CMTemplate.FAILURE;
  80. log.error(e.getMessage());
  81. }finally{
  82. return state;
  83. }
  84. }
  85. /**
  86. *
  87. * @param sql
  88. * @param args
  89. * @return -1:异常;>0:返回成功更新的结果
  90. * @throws SQLException
  91. */
  92. public int update(String sql,Object...args) throws SQLException{
  93. Connection conn=null;
  94. PreparedStatement ps=null;
  95. try {
  96. conn=DBUtils.getConnection();
  97. ps=conn.prepareStatement(sql);
  98. //设置占位符的参数
  99. if(args!=null){
  100. for(int i=0;i<args.length;i++){
  101. ps.setObject(i+1, args[i]);
  102. }
  103. }
  104. return ps.executeUpdate();
  105. } catch (SQLException e) {
  106. e.printStackTrace();
  107. throw new SQLStringException(this.getClass()+":update(String sql,Object...args);sql语句异常");
  108. }finally{
  109. DBUtils.close(null, ps, conn);
  110. }
  111. }
  112. /**
  113. * 查询
  114. * @param sql
  115. * @param handler
  116. * @param args
  117. * @return null:查询异常
  118. * @throws SQLStringException
  119. */
  120. public Object query(String sql,CMHandlerResultSet handler,Object...args) throws SQLStringException{
  121. Connection conn=null;
  122. PreparedStatement ps=null;
  123. ResultSet rs=null;
  124. try {
  125. conn=DBUtils.getConnection();
  126. ps=conn.prepareStatement(sql);
  127. if(args!=null){
  128. for(int i=0;i<args.length;i++){
  129. ps.setObject(i+1,args[i]);
  130. }
  131. }
  132. rs=ps.executeQuery();
  133. return handler.doHandler(rs);
  134. } catch (SQLException e) {
  135. throw new SQLStringException(this.getClass()+": query(String sql,CMHandlerResultSet handler,Object...args);sql语句异常");
  136. }finally{
  137. DBUtils.close(rs,ps,conn);
  138. }
  139. }
  140. /**
  141. * 查询指定的字段
  142. * @param sql
  143. * @param list
  144. * @param args
  145. * @return
  146. * @throws SQLStringException
  147. */
  148. public List<Map<String,String >> queryToMap(String sql,List<String> list,Object...args) throws SQLStringException{
  149. Connection conn=null;
  150. PreparedStatement ps=null;
  151. ResultSet rs=null;
  152. try {
  153. conn=DBUtils.getConnection();
  154. ps=conn.prepareStatement(sql);
  155. if(args!=null){
  156. for(int i=0;i<args.length;i++){
  157. ps.setObject(i+1,args[i]);
  158. }
  159. }
  160. rs=ps.executeQuery();
  161. List<Map<String,String>> listMap=new ArrayList<Map<String,String>>();
  162. while(rs.next()){
  163. Map<String ,String> map=new HashMap<String, String>();
  164. for(String key:list){
  165. if(rs.getObject(key)==null){
  166. map.put(key,"0");
  167. }else{
  168. map.put(key, rs.getObject(key).toString());
  169. }
  170. }
  171. listMap.add(map);
  172. }
  173. return listMap;
  174. } catch (SQLException e) {
  175. throw new SQLStringException(this.getClass()+": query(String sql,CMHandlerResultSet handler,Object...args);sql语句异常");
  176. }finally{
  177. DBUtils.close(rs,ps,conn);
  178. }
  179. }
  180. public List<String> queryToList(String sql,Object...args)throws SQLStringException{
  181. Connection conn=null;
  182. PreparedStatement ps=null;
  183. ResultSet rs=null;
  184. try {
  185. conn=DBUtils.getConnection();
  186. ps=conn.prepareStatement(sql);
  187. if(args!=null){
  188. for(int i=0;i<args.length;i++){
  189. ps.setObject(i+1,args[i]);
  190. }
  191. }
  192. rs=ps.executeQuery();
  193. List<String> list=new ArrayList<String>();
  194. while(rs.next()){
  195. list.add(rs.getObject(1).toString());
  196. }
  197. return list;
  198. } catch (SQLException e) {
  199. throw new SQLStringException(this.getClass()+": query(String sql,CMHandlerResultSet handler,Object...args);sql语句异常");
  200. }finally{
  201. DBUtils.close(rs,ps,conn);
  202. }
  203. }
  204. public List<Map<String,String >> queryMoreTypeToMap(String sql,List<String> list,Object...args) throws SQLStringException{
  205. Connection conn=null;
  206. PreparedStatement ps=null;
  207. ResultSet rs=null;
  208. try {
  209. conn=DBUtils.getConnection();
  210. ps=conn.prepareStatement(sql);
  211. if(args!=null){
  212. for(int i=0;i<args.length;i++){
  213. ps.setObject(i+1,args[i]);
  214. }
  215. }
  216. rs=ps.executeQuery();
  217. List<Map<String,String>> listMap=new ArrayList<Map<String,String>>();
  218. while(rs.next()){
  219. Map<String ,String> map=new HashMap<String, String>();
  220. for(String key:list){
  221. if(rs.getObject(key)==null){
  222. map.put(key,"0");
  223. }else{
  224. if(rs.getObject(key) instanceof Integer) map.put(key,String.valueOf((Integer)rs.getObject(key)));
  225. if(rs.getObject(key) instanceof java.sql.Date)
  226. map.put(key, DateUtil.toString((java.sql.Date)rs.getObject(key)));
  227. if(rs.getObject(key) instanceof java.sql.Timestamp)
  228. map.put(key, DateUtil.toString(new Date(((java.sql.Timestamp)(rs.getObject(key))).getTime())));
  229. if(rs.getObject(key) instanceof java.sql.Clob)
  230. map.put(key, StringUtils.clobToString((java.sql.Clob)(rs.getObject(key))));
  231. if(rs.getObject(key) instanceof String)
  232. map.put(key, rs.getObject(key).toString());
  233. if(rs.getObject(key) instanceof Number)
  234. map.put(key, String.valueOf(rs.getObject(key)));
  235. }
  236. }
  237. listMap.add(map);
  238. }
  239. return listMap;
  240. } catch (SQLException e) {
  241. throw new SQLStringException(this.getClass()+": query(String sql,CMHandlerResultSet handler,Object...args);sql语句异常");
  242. }finally{
  243. DBUtils.close(rs,ps,conn);
  244. }
  245. }
  246. public List<Map<String,String >> queryMoreTypeToMap(String sql,Object...args) throws SQLStringException{
  247. Connection conn=null;
  248. PreparedStatement ps=null;
  249. ResultSet rs=null;
  250. try {
  251. conn=DBUtils.getConnection();
  252. ps=conn.prepareStatement(sql);
  253. if(args!=null){
  254. for(int i=0;i<args.length;i++){
  255. ps.setObject(i+1,args[i]);
  256. }
  257. }
  258. rs=ps.executeQuery();
  259. ResultSetMetaData metaData=ps.getMetaData();
  260. int count=metaData.getColumnCount();
  261. List<Map<String,String>> listMap=new ArrayList<Map<String,String>>();
  262. while(rs.next()){
  263. Map<String ,String> map=new HashMap<String, String>();
  264. for(int index=0;index<count;index++){
  265. String key=metaData.getColumnLabel(index+1);
  266. if(rs.getObject(key)==null){
  267. map.put(key,"0");
  268. }else{
  269. if(rs.getObject(key) instanceof Integer) map.put(key,String.valueOf((Integer)rs.getObject(key)));
  270. if(rs.getObject(key) instanceof java.sql.Date)
  271. map.put(key, DateUtil.toString((java.sql.Date)rs.getObject(key)));
  272. if(rs.getObject(key) instanceof java.sql.Timestamp)
  273. map.put(key, DateUtil.toString(new Date(((java.sql.Timestamp)(rs.getObject(key))).getTime())));
  274. if(rs.getObject(key) instanceof java.sql.Clob)
  275. map.put(key, StringUtils.clobToString((java.sql.Clob)(rs.getObject(key))));
  276. if(rs.getObject(key) instanceof String)
  277. map.put(key, rs.getObject(key).toString());
  278. if(rs.getObject(key) instanceof Number)
  279. map.put(key, String.valueOf(rs.getObject(key)));
  280. }
  281. }
  282. listMap.add(map);
  283. }
  284. return listMap;
  285. } catch (SQLException e) {
  286. throw new SQLStringException(this.getClass()+": query(String sql,CMHandlerResultSet handler,Object...args);sql语句异常");
  287. }finally{
  288. DBUtils.close(rs,ps,conn);
  289. }
  290. }
  291. public List<Map<String,String >> queryMoreTypeToMapByCallableStatement(String sql,Object...args) throws SQLStringException{
  292. Connection conn=null;
  293. CallableStatement ps=null;
  294. ResultSet rs=null;
  295. try {
  296. conn=DBUtils.getConnection();
  297. ps=conn.prepareCall(sql);
  298. if(args!=null){
  299. for(int i=0;i<args.length;i++){
  300. ps.setObject(i+1,args[i]);
  301. }
  302. }
  303. rs=ps.executeQuery();
  304. ResultSetMetaData metaData=ps.getMetaData();
  305. int count=metaData.getColumnCount();
  306. List<Map<String,String>> listMap=new ArrayList<Map<String,String>>();
  307. while(rs.next()){
  308. Map<String ,String> map=new HashMap<String, String>();
  309. for(int index=0;index<count;index++){
  310. String key=metaData.getColumnLabel(index+1);
  311. if(rs.getObject(key)==null){
  312. map.put(key,"0");
  313. }else{
  314. if(rs.getObject(key) instanceof Integer) map.put(key,String.valueOf((Integer)rs.getObject(key)));
  315. if(rs.getObject(key) instanceof java.sql.Date)
  316. map.put(key, DateUtil.toString((java.sql.Date)rs.getObject(key)));
  317. if(rs.getObject(key) instanceof java.sql.Timestamp)
  318. map.put(key, DateUtil.toString(new Date(((java.sql.Timestamp)(rs.getObject(key))).getTime())));
  319. if(rs.getObject(key) instanceof java.sql.Clob)
  320. map.put(key, StringUtils.clobToString((java.sql.Clob)(rs.getObject(key))));
  321. if(rs.getObject(key) instanceof String)
  322. map.put(key, rs.getObject(key).toString());
  323. if(rs.getObject(key) instanceof Number)
  324. map.put(key, String.valueOf(rs.getObject(key)));
  325. }
  326. }
  327. listMap.add(map);
  328. }
  329. return listMap;
  330. } catch (SQLException e) {
  331. throw new SQLStringException(this.getClass()+": query(String sql,CMHandlerResultSet handler,Object...args);sql语句异常");
  332. }finally{
  333. DBUtils.close(rs,ps,conn);
  334. }
  335. }
  336. public Connection getConnection() {
  337. return connection;
  338. }
  339. public void setConnection(Connection connection) {
  340. this.connection = connection;
  341. }
  342. public static void main(String[] args) throws SQLException {
  343. String str="insert into NWYJ . DEPARTMENT_CEDIAN ( XZDW ,CDYCM ,CDID ,FD_OBJECTID ) VALUES(?,?,?,?);";
  344. CMTemplate cmt=new CMTemplate();
  345. /* cmt.update(str,"佛山","佛山地区负荷地区总负荷","121878665083486867",UUIDUtil.getFdObjectId());
  346. cmt.update(str,"中山","中山地区负荷地区总负荷","121878665083486982",UUIDUtil.getFdObjectId());
  347. cmt.update(str,"清远","清远地区负荷地区总负荷 ","121878665083486960",UUIDUtil.getFdObjectId());
  348. cmt.update(str,"珠海","珠海地区负荷地区总负荷","121878665083486845",UUIDUtil.getFdObjectId());
  349. cmt.update(str,"阳江","阳江地区负荷地区总负荷","121878665083486950",UUIDUtil.getFdObjectId());
  350. cmt.update(str,"江门","江门地区负荷地区总负荷","121878665083486877",UUIDUtil.getFdObjectId());
  351. cmt.update(str,"汕头","汕头地区负荷地区总负荷","121878665083486855",UUIDUtil.getFdObjectId());
  352. cmt.update(str,"东莞","东莞地区负荷地区总负荷","121878665083486971",UUIDUtil.getFdObjectId());
  353. cmt.update(str,"肇庆","肇庆地区负荷地区总负荷","121878665083486907",UUIDUtil.getFdObjectId());
  354. cmt.update(str,"潮州","潮州地区负荷地区总负荷","121878665083486990",UUIDUtil.getFdObjectId());
  355. cmt.update(str,"揭阳","揭阳地区负荷地区总负荷 ","121878665083487000",UUIDUtil.getFdObjectId());
  356. cmt.update(str,"梅州","梅州地区负荷地区总负荷","121878665083486925",UUIDUtil.getFdObjectId());
  357. cmt.update(str,"河源","河源地区负荷地区总负荷","121878665083486942",UUIDUtil.getFdObjectId());
  358. cmt.update(str,"云浮","云浮地区负荷地区总负荷 ","121878665083487008",UUIDUtil.getFdObjectId());
  359. cmt.update(str,"韶关","韶关地区负荷地区总负荷","121878665083486824",UUIDUtil.getFdObjectId());
  360. cmt.update(str,"汕尾","汕尾地区负荷地区总负荷 ","121878665083486934",UUIDUtil.getFdObjectId());
  361. cmt.update(str,"茂名","茂名地区负荷地区总负荷","121878665083486897",UUIDUtil.getFdObjectId());
  362. cmt.update(str,"湛江","湛江地区负荷地区总负荷 ","121878665083486887",UUIDUtil.getFdObjectId());
  363. cmt.update(str,"惠州","惠州地区负荷地区总负荷","121878665083486918",UUIDUtil.getFdObjectId());*/
  364. cmt.update(str,"","统调负荷有功","121878665083489910",UUIDUtil.getFdObjectId());
  365. }
  366. }