KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > dbtags > statement > StatementImplTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.taglibs.dbtags.statement;
17
18 import java.io.IOException JavaDoc;
19 import java.sql.Connection JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Statement JavaDoc;
23
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
26
27 /**
28  * <p>JSP tag statement. According to the TEI, the java.sql.Statement
29  * object specified in the "id" attribute is available within the scope
30  * of the statement tags. </p>
31  *
32  * <p>JSP Tag Lib Descriptor
33  * <pre>
34  * &lt;name>statement&lt;/name>
35  * &lt;tagclass>org.apache.taglibs.dbtags.statement.StatementImplTag&lt;/tagclass>
36  * &lt;teiclass>org.apache.taglibs.dbtags.connection.StatementTEI&lt;/teiclass>
37  * &lt;bodycontent>JSP&lt;/bodycontent>
38  * &lt;info>JSP tag statement, uses the enclosed query and resultset/execute
39  * tags to perform a database operation.&lt;/info>
40  * &lt;attribute>
41  * &lt;name>id&lt;/name>
42  * &lt;required>true&lt;/required>
43  * &lt;rtexprvalue>false&lt;/rtexprvalue>
44  * &lt;/attribute>
45  * &lt;attribute>
46  * &lt;name>conn&lt;/name>
47  * &lt;required>true&lt;/required>
48  * &lt;rtexprvalue>false&lt;/rtexprvalue>
49  * &lt;/attribute>
50  * </pre>
51  *
52  * @author Morgan Delagrange
53  */

54 public class StatementImplTag extends BodyTagSupport JavaDoc implements StatementTag{
55
56   private Statement JavaDoc _statement = null;
57   private String JavaDoc _query = null;
58   private String JavaDoc _connId = null;
59
60   // all the public methods are Javadoced
61
// in their interfaces
62

63   public void setQuery(String JavaDoc query) {
64     _query = query;
65   }
66
67   public void setConn(String JavaDoc connId) {
68     _connId = connId;
69   }
70
71   public void executeUpdate() throws SQLException JavaDoc {
72     _statement.executeUpdate(_query);
73   }
74
75   public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc {
76     return _statement.executeQuery(_query);
77   }
78
79   public int doStartTag() throws JspTagException JavaDoc {
80     if (_connId == null) {
81       throw new JspTagException JavaDoc("Connection id has not been set.");
82     }
83
84     try {
85       Connection JavaDoc conn = (Connection JavaDoc)pageContext.findAttribute(_connId);
86       if(conn == null) {
87         throw new JspTagException JavaDoc("There is no such connection'"+_connId+"'");
88       }
89       _statement = createStatement ( conn );
90       pageContext.setAttribute(getId(), _statement);
91     } catch (SQLException JavaDoc e) {
92       throw new JspTagException JavaDoc(e.toString());
93     }
94
95     return EVAL_BODY_TAG;
96   }
97
98
99   /**
100    * Let subclass redefine how to create the statement
101    */

102   protected Statement JavaDoc createStatement ( Connection JavaDoc theConnection ) throws SQLException JavaDoc
103   {
104     return theConnection.createStatement();
105   }
106
107
108   public int doAfterBody() throws JspTagException JavaDoc
109   {
110       // Automatically trim the contents of this tag
111
try {
112         getPreviousOut().write(getBodyContent().getString().trim());
113       } catch (IOException JavaDoc e) {
114         throw new JspTagException JavaDoc(e.toString());
115       }
116       return EVAL_PAGE;
117   }
118
119   public int doEndTag() {
120     pageContext.removeAttribute(getId());
121
122     try {
123       _statement.close();
124     } catch (SQLException JavaDoc e) {
125       // it's not a fatal error if we can't close the statement
126
e.printStackTrace();
127     }
128
129     return EVAL_PAGE;
130   }
131
132   public void release() {
133     _connId=null;
134     _statement = null;
135     _query=null;
136   }
137
138 }
139
Popular Tags