KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.apache.taglibs.dbtags.preparedstatement;
18
19 import java.lang.reflect.Field JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21
22 import javax.servlet.jsp.JspTagException JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25 /**
26  * Base class for all the setter tags without bodies
27  * in the preparedstatement package.
28  *
29  * @author Morgan Delagrange
30  */

31 public class BaseSetterTag extends TagSupport JavaDoc {
32   
33   protected int _position = 1;
34   protected String JavaDoc _attributeName = null;
35   
36   /**
37    * Sets the column number of the prepared statement.
38    *
39    * @param position column index
40    */

41   public void setPosition(int position) {
42     _position = position;
43   }
44   
45   /**
46    * Name of the page attribute that will be assigned
47    * to the statement.
48    *
49    * @param attributeName
50    * attribute name
51    */

52   public void setName(String JavaDoc attributeName) {
53     _attributeName = attributeName;
54   }
55       
56   /**
57    * Gets the page attribute for the tag
58    *
59    * @param name name of the attribute
60    * @return the page attribute
61    * @exception JspTagException
62    * thrown when the page attribute does not exist
63    */

64   protected Object JavaDoc getAttribute(String JavaDoc name)
65       throws JspTagException JavaDoc {
66     Object JavaDoc object = pageContext.getAttribute(name);
67     
68     if (object == null) {
69       throw new JspTagException JavaDoc("attribute " + name + " does not exist");
70     }
71       
72     return object;
73   }
74
75   /**
76    * get the PreparedStatement from the enclosing tag
77    *
78    * @return the PreparedStatement
79    * @exception JspTagException
80    * thrown if no PreparedStatement exists
81    */

82   protected PreparedStatement JavaDoc getPreparedStatement()
83       throws JspTagException JavaDoc {
84     try {
85       PreparedStatementImplTag stmtTag =
86       (PreparedStatementImplTag) findAncestorWithClass(this, Class.forName("org.apache.taglibs.dbtags.preparedstatement.PreparedStatementImplTag"));
87       return stmtTag.getPreparedStatement();
88     } catch (ClassNotFoundException JavaDoc e) {
89       throw new JspTagException JavaDoc(e.toString());
90     }
91   }
92   
93   /**
94    * Perfoms reflection to translate a String name to one of
95    * the int values of java.sql.Types
96    *
97    * @param fieldName name of the field in java.sql.Types
98    * @return int value of the sql type
99    * @exception JspTagException
100    * thrown if the sql type does not exist, or if
101    * reflection fails
102    */

103   protected int getSqlTypeForFieldName(String JavaDoc fieldName)
104       throws JspTagException JavaDoc {
105     try {
106       Class JavaDoc typesClass = Class.forName("java.sql.Types");
107       Field JavaDoc typeField = typesClass.getField(fieldName);
108       return typeField.getInt(null);
109     } catch (ClassNotFoundException JavaDoc e) {
110       throw new JspTagException JavaDoc(e.toString());
111     } catch (NoSuchFieldException JavaDoc e) {
112       throw new JspTagException JavaDoc("Illegal argument: setnull tag could not find = '" +
113                          fieldName + "' field in the java.sql.Types class" + e.toString());
114     } catch (IllegalAccessException JavaDoc e) {
115       throw new JspTagException JavaDoc(e.toString());
116     }
117   }
118
119   public void release() {
120     _position = 1;
121     _attributeName = null;
122   }
123 }
124
Popular Tags