PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/svnadmin/dao/PjGrDao.java

http://jsvnadmin.googlecode.com/
Java | 206 lines | 140 code | 18 blank | 48 comment | 5 complexity | 229bf9e572dfb0bca70aff7fc5afe22f MD5 | raw file
  1. package org.svnadmin.dao;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import org.springframework.stereotype.Repository;
  9. import org.svnadmin.entity.PjGr;
  10. /**
  11. * 项目组
  12. *
  13. * @author <a href="mailto:yuanhuiwu@gmail.com">Huiwu Yuan</a>
  14. *
  15. */
  16. @Repository(PjGrDao.BEAN_NAME)
  17. public class PjGrDao extends Dao {
  18. /**
  19. * Bean名称
  20. */
  21. public static final String BEAN_NAME = "pjGrDao";
  22. /**
  23. * @param pj
  24. * 项目
  25. * @param gr
  26. * 组
  27. * @return 项目组
  28. */
  29. public PjGr get(String pj, String gr) {
  30. String sql = "select pj,gr,des from pj_gr where pj = ? and gr=?";
  31. Connection conn = null;
  32. PreparedStatement pstmt = null;
  33. ResultSet rs = null;
  34. try {
  35. conn = this.getConnection();
  36. pstmt = conn.prepareStatement(sql);
  37. int index = 1;
  38. pstmt.setString(index++, pj);
  39. pstmt.setString(index++, gr);
  40. rs = pstmt.executeQuery();
  41. if (rs.next()) {
  42. return readPjGr(rs);
  43. }
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. throw new RuntimeException(e);
  47. } finally {
  48. this.close(rs, pstmt, conn);
  49. }
  50. return null;
  51. }
  52. /**
  53. * @param pj
  54. * 项目
  55. * @return 项目组列表
  56. */
  57. public List<PjGr> getList(String pj) {
  58. String sql = "select pj,gr,des from pj_gr where pj=? order by pj,gr";
  59. List<PjGr> list = new ArrayList<PjGr>();
  60. Connection conn = null;
  61. PreparedStatement pstmt = null;
  62. ResultSet rs = null;
  63. try {
  64. conn = this.getConnection();
  65. pstmt = conn.prepareStatement(sql);
  66. int index = 1;
  67. pstmt.setString(index++, pj);
  68. rs = pstmt.executeQuery();
  69. while (rs.next()) {
  70. list.add(readPjGr(rs));
  71. }
  72. return list;
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. throw new RuntimeException(e);
  76. } finally {
  77. this.close(rs, pstmt, conn);
  78. }
  79. }
  80. /**
  81. * @param rs
  82. * ResultSet
  83. * @return 项目组
  84. * @throws SQLException
  85. * jdbc异常
  86. */
  87. PjGr readPjGr(ResultSet rs) throws SQLException {
  88. PjGr result = new PjGr();
  89. result.setPj(rs.getString("pj"));
  90. result.setGr(rs.getString("gr"));
  91. result.setDes(rs.getString("des"));
  92. return result;
  93. }
  94. /**
  95. * 删除
  96. *
  97. * @param pj
  98. * 项目
  99. * @param gr
  100. * 组
  101. */
  102. public void delete(String pj, String gr) {
  103. String sql = "delete from pj_gr where pj = ? and gr=?";
  104. Connection conn = null;
  105. PreparedStatement pstmt = null;
  106. try {
  107. conn = this.getConnection();
  108. pstmt = conn.prepareStatement(sql);
  109. int index = 1;
  110. pstmt.setString(index++, pj);
  111. pstmt.setString(index++, gr);
  112. pstmt.executeUpdate();
  113. } catch (SQLException e) {
  114. e.printStackTrace();
  115. throw new RuntimeException(e);
  116. } finally {
  117. this.close(null, pstmt, conn);
  118. }
  119. }
  120. /**
  121. * 删除
  122. *
  123. * @param pj
  124. * 项目
  125. */
  126. public void deletePj(String pj) {
  127. String sql = "delete from pj_gr where pj = ?";
  128. Connection conn = null;
  129. PreparedStatement pstmt = null;
  130. try {
  131. conn = this.getConnection();
  132. pstmt = conn.prepareStatement(sql);
  133. int index = 1;
  134. pstmt.setString(index++, pj);
  135. pstmt.executeUpdate();
  136. } catch (SQLException e) {
  137. e.printStackTrace();
  138. throw new RuntimeException(e);
  139. } finally {
  140. this.close(null, pstmt, conn);
  141. }
  142. }
  143. /**
  144. * 保存
  145. *
  146. * @param pjGr
  147. * 项目组
  148. */
  149. public void save(PjGr pjGr) {
  150. if (this.get(pjGr.getPj(), pjGr.getGr()) == null) {
  151. String sql = "insert into pj_gr (pj,gr,des) values (?,?,?)";
  152. Connection conn = null;
  153. PreparedStatement pstmt = null;
  154. try {
  155. conn = this.getConnection();
  156. pstmt = conn.prepareStatement(sql);
  157. int index = 1;
  158. pstmt.setString(index++, pjGr.getPj());
  159. pstmt.setString(index++, pjGr.getGr());
  160. pstmt.setString(index++, pjGr.getDes());
  161. pstmt.executeUpdate();
  162. } catch (SQLException e) {
  163. e.printStackTrace();
  164. throw new RuntimeException(e);
  165. } finally {
  166. this.close(null, pstmt, conn);
  167. }
  168. } else {
  169. String sql = "update pj_gr set des=? where pj = ? and gr=?";
  170. Connection conn = null;
  171. PreparedStatement pstmt = null;
  172. try {
  173. conn = this.getConnection();
  174. pstmt = conn.prepareStatement(sql);
  175. int index = 1;
  176. pstmt.setString(index++, pjGr.getDes());
  177. pstmt.setString(index++, pjGr.getPj());
  178. pstmt.setString(index++, pjGr.getGr());
  179. pstmt.executeUpdate();
  180. } catch (SQLException e) {
  181. e.printStackTrace();
  182. throw new RuntimeException(e);
  183. } finally {
  184. this.close(null, pstmt, conn);
  185. }
  186. }
  187. }
  188. }