KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > dbtags > preparedstatement > PreparedStatementImplTag


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.preparedstatement;
17
18 import java.io.IOException JavaDoc;
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
26
27 import org.apache.taglibs.dbtags.statement.StatementTag;
28
29 /**
30  * <p>JSP tag preparedstatement. According to the TEI, the preparedstatemnt
31  * object specified in the "id" attribute is available within the scope
32  * of the preparedstatement tags. However, this is not quite the case.
33  * Technically, the object is not added to the page context until after
34  * the query tag is executed, because PreparedStatement objects cannot
35  * be instantiated without a query.</p>
36  *
37  * <p>JSP Tag Lib Descriptor
38  * <pre>
39  * &lt;name>preparedStatement&lt;/name>
40  * &lt;tagclass>org.apache.taglibs.dbtags.preparedstatement.PreparedStatementImplTag&lt;/tagclass>
41  * &lt;teiclass>org.apache.taglibs.dbtags.connection.PreparedStatementTEI&lt;/teiclass>
42  * &lt;bodycontent>JSP&lt;/bodycontent>
43  * &lt;info>JSP tag preparedstatement, used the enclosed query,
44  * resultset/execute and set* tags to perform a database operation.&lt;/info>
45  * &lt;attribute>
46  * &lt;name>id&lt;/name>
47  * &lt;required>true&lt;/required>
48  * &lt;rtexprvalue>false&lt;/rtexprvalue>
49  * &lt;/attribute>
50  * &lt;attribute>
51  * &lt;name>conn&lt;/name>
52  * &lt;required>true&lt;/required>
53  * &lt;rtexprvalue>false&lt;/rtexprvalue>
54  * &lt;/attribute>
55  * </pre>
56  *
57  * @author Morgan Delagrange
58  */

59 public class PreparedStatementImplTag extends BodyTagSupport JavaDoc
60     implements StatementTag {
61
62   private String JavaDoc _connId = null;
63   private PreparedStatement JavaDoc _statement = null;
64
65   public void setQuery(String JavaDoc query) throws SQLException JavaDoc, JspTagException JavaDoc {
66     Connection JavaDoc conn = (Connection JavaDoc)pageContext.findAttribute(_connId);
67     if(conn == null) {
68       throw new JspTagException JavaDoc("There is no such connection'"+_connId+"'");
69     }
70     _statement = createStatement ( conn, query );
71     pageContext.setAttribute(getId(), _statement);
72   }
73
74   /**
75    * Let subclass redefine how to create the statement
76    */

77   protected PreparedStatement JavaDoc createStatement ( Connection JavaDoc theConnection, String JavaDoc theQuery ) throws SQLException JavaDoc
78   {
79     return theConnection.prepareStatement ( theQuery );
80   }
81
82
83   /**
84    *
85    * @param connId
86    */

87   public void setConn(String JavaDoc connId) {
88     _connId = connId;
89   }
90
91   /**
92    * Get the PreparedStatement contained within this tag
93    *
94    * @return the PreparedStatement
95    */

96   public PreparedStatement JavaDoc getPreparedStatement() {
97     return _statement;
98   }
99
100   public void executeUpdate() throws SQLException JavaDoc {
101     _statement.executeUpdate();
102   }
103
104   public ResultSet JavaDoc executeQuery() throws SQLException JavaDoc {
105     return _statement.executeQuery();
106   }
107
108   public int doStartTag() throws JspTagException JavaDoc {
109     if (_connId == null) {
110       throw new JspTagException JavaDoc("Connection id has not been set.");
111     }
112
113     // _statement is set by the setQuery tag, not by the start tag.
114
// Unlike Statements, PreparedStatements cannot be instantiated
115
// without a query.
116

117     return EVAL_BODY_TAG;
118   }
119
120   public int doAfterBody() throws JspTagException JavaDoc
121   {
122     // Automatically trim the contents of this tag
123
try {
124       getPreviousOut().write(getBodyContent().getString().trim());
125     } catch (IOException JavaDoc e) {
126       throw new JspTagException JavaDoc(e.toString());
127     }
128     return EVAL_PAGE;
129   }
130
131   public int doEndTag() {
132     pageContext.removeAttribute(getId());
133
134     try {
135       _statement.close();
136     } catch (SQLException JavaDoc e) {
137       // not a fatal error
138
e.printStackTrace();
139     }
140
141     return EVAL_PAGE;
142   }
143
144   public void release() {
145     _connId = null;
146     _statement = null;
147   }
148 }
149
Popular Tags