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