01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU Library General Public License for more details.
11: *
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15: */
16: package web.struts;
17:
18: import java.sql.*;
19: import javax.servlet.ServletContext;
20: import javax.sql.DataSource;
21:
22: import org.apache.struts.Globals;
23: import org.apache.struts.action.ActionForm;
24:
25: /**
26: * Formbean类的扩展
27: * @author Winter Lau
28: */
29: public abstract class ActionFormExtend extends ActionForm {
30:
31: /**
32: * 获取到默认数据库的连接
33: * @return
34: * @throws SQLException
35: */
36: protected Connection getConnection() throws SQLException {
37: ServletContext context = servlet.getServletContext();
38: DataSource dataSource = (DataSource) context
39: .getAttribute(Globals.DATA_SOURCE_KEY);
40: return dataSource.getConnection();
41: }
42:
43: /**
44: * 获取到指定数据库的连接
45: * @return
46: * @throws SQLException
47: */
48: protected Connection getConnection(String key) throws SQLException {
49: ServletContext context = servlet.getServletContext();
50: DataSource dataSource = (DataSource) context.getAttribute(key);
51: return dataSource.getConnection();
52: }
53:
54: /**
55: * 释放数据库资料
56: * @param rs
57: * @param stmt
58: * @param conn
59: */
60: protected void close(ResultSet rs, Statement stmt, Connection conn) {
61: if (rs != null)
62: try {
63: rs.close();
64: } catch (Exception exception) {
65: }
66: if (stmt != null)
67: try {
68: stmt.close();
69: } catch (Exception exception1) {
70: }
71: if (conn != null)
72: try {
73: conn.close();
74: } catch (Exception exception2) {
75: }
76: }
77: }
|