5f5e3846a8743c6afeaa295752f57e220d8d764b.svn-base 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.sinosoft.em.evaluate;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.apache.commons.logging.Log;
  12. import org.apache.commons.logging.LogFactory;
  13. import com.persistence.DbConnection;
  14. /**
  15. * 用于数据库操作
  16. * @author len
  17. *
  18. */
  19. public class DbHander {
  20. private static Log log = LogFactory.getLog(DbHander.class);
  21. private DbConnection dbConn = new DbConnection();
  22. private Connection conn = null;
  23. private PreparedStatement prst = null;
  24. public DbHander(){
  25. //createDbConn();
  26. }
  27. public void createDbConn(){
  28. try {
  29. if(conn==null || conn.isClosed())
  30. conn = dbConn.getConnection();
  31. } catch (ClassNotFoundException e) {
  32. e.printStackTrace();
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. log.error("DbHander 连接数据失败", e);
  36. }
  37. }
  38. public void destroyDbConn() {
  39. try {
  40. if(prst!=null)
  41. prst.close();
  42. if(conn!=null && !conn.isClosed())
  43. conn.close();
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. log.error("DbHander 连接关闭失败", e);
  47. }
  48. }
  49. public List<Map<String,String>> queryForList(String sql,Object [] args0){
  50. createDbConn();
  51. List<Map<String,String>> result = new ArrayList<Map<String,String>>();
  52. try {
  53. prst = conn.prepareStatement(sql);
  54. for(int i=0;i<args0.length;i++){
  55. prst.setString(i+1,String.valueOf(args0[i]));
  56. }
  57. ResultSet rs = prst.executeQuery();
  58. ResultSetMetaData rsmd = rs.getMetaData();
  59. int count = rsmd.getColumnCount();
  60. //String field = rsmd.getColumnName(1);
  61. while(rs.next()){
  62. Map<String,String> map = new HashMap<String, String>();
  63. for(int i=0;i<count;i++){
  64. map.put(rsmd.getColumnName(i+1), rs.getString(rsmd.getColumnName(i+1)));
  65. }
  66. result.add(map);
  67. }
  68. rs.close();
  69. destroyDbConn();
  70. } catch (SQLException e) {
  71. log.error("数据库查询失败!");
  72. e.printStackTrace();
  73. }
  74. return result;
  75. }
  76. public List<Map<String,String>> queryForList(String sql){
  77. createDbConn();
  78. List<Map<String,String>> result = new ArrayList<Map<String,String>>();
  79. try {
  80. prst = conn.prepareStatement(sql);
  81. ResultSet rs = prst.executeQuery();
  82. ResultSetMetaData rsmd = rs.getMetaData();
  83. int count = rsmd.getColumnCount();
  84. //String field = rsmd.getColumnName(1);
  85. while(rs.next()){
  86. Map<String,String> map = new HashMap<String, String>();
  87. for(int i=0;i<count;i++){
  88. map.put(rsmd.getColumnName(i+1), rs.getString(rsmd.getColumnName(i+1)));
  89. }
  90. result.add(map);
  91. }
  92. rs.close();
  93. destroyDbConn();
  94. } catch (SQLException e) {
  95. log.error("数据库查询失败!");
  96. e.printStackTrace();
  97. }
  98. return result;
  99. }
  100. public ResultSet query(String sql,Object [] args){
  101. createDbConn();
  102. ResultSet rs = null;
  103. try {
  104. prst = conn.prepareStatement(sql);
  105. for(int i=0;i<args.length;i++){
  106. prst.setString(i+1,String.valueOf(args[i]));
  107. }
  108. rs = prst.executeQuery();
  109. } catch (SQLException e) {
  110. log.error("数据库查询失败!");
  111. e.printStackTrace();
  112. }
  113. return rs;
  114. }
  115. public ResultSet query(String sql){
  116. createDbConn();
  117. ResultSet rs = null;
  118. try {
  119. prst = conn.prepareStatement(sql);
  120. rs = prst.executeQuery();
  121. } catch (SQLException e) {
  122. log.error("数据库查询失败!");
  123. e.printStackTrace();
  124. }
  125. return rs;
  126. }
  127. public int update(String sql,Object [] args){
  128. createDbConn();
  129. int result = 0;
  130. try {
  131. prst = conn.prepareStatement(sql);
  132. for(int i=0;i<args.length;i++){
  133. prst.setString(i+1,String.valueOf(args[i]));
  134. }
  135. result = prst.executeUpdate();
  136. } catch (SQLException e) {
  137. log.error("数据库更新失败!");
  138. e.printStackTrace();
  139. }finally{
  140. destroyDbConn();
  141. }
  142. return result;
  143. }
  144. public int update(String sql){
  145. createDbConn();
  146. int result = 0;
  147. try {
  148. prst = conn.prepareStatement(sql);
  149. result = prst.executeUpdate();
  150. } catch (SQLException e) {
  151. log.error("数据库更新失败!");
  152. e.printStackTrace();
  153. }finally{
  154. destroyDbConn();
  155. }
  156. return result;
  157. }
  158. public int insert(String sql,Object [] args){
  159. createDbConn();
  160. int result = 0;
  161. try {
  162. prst = conn.prepareStatement(sql);
  163. for(int i=0;i<args.length;i++){
  164. prst.setString(i+1,String.valueOf(args[i]));
  165. }
  166. result = prst.executeUpdate();
  167. } catch (SQLException e) {
  168. log.error("数据库更新失败!");
  169. e.printStackTrace();
  170. }finally{
  171. destroyDbConn();
  172. }
  173. return result;
  174. }
  175. public int insert(String sql){
  176. createDbConn();
  177. int result = 0;
  178. try {
  179. prst = conn.prepareStatement(sql);
  180. result = prst.executeUpdate();
  181. } catch (SQLException e) {
  182. log.error("数据库更新失败!");
  183. e.printStackTrace();
  184. }finally{
  185. destroyDbConn();
  186. }
  187. return result;
  188. }
  189. }