KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > web > struts > ActionFormExtend


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * 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 JavaDoc;
20 import javax.sql.DataSource JavaDoc;
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 JavaDoc context = servlet.getServletContext();
38         DataSource JavaDoc dataSource = (DataSource JavaDoc) 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 JavaDoc key) throws SQLException {
49         ServletContext JavaDoc context = servlet.getServletContext();
50         DataSource JavaDoc dataSource = (DataSource JavaDoc) 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 JavaDoc exception) {
65             }
66         if (stmt != null)
67             try {
68                 stmt.close();
69             } catch (Exception JavaDoc exception1) {
70             }
71         if (conn != null)
72             try {
73                 conn.close();
74             } catch (Exception JavaDoc exception2) {
75             }
76     }
77 }
Popular Tags