KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > dbtags > resultset > GetColumnTag


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.resultset;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.sql.Clob JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.ResultSetMetaData JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Types JavaDoc;
26
27 import javax.servlet.jsp.JspTagException JavaDoc;
28
29 /**
30  * <p>Get the value of a database column as a String.</p>
31  *
32  * <p>JSP Tag Lib Descriptor
33  * <pre>
34  * &lt;name>getString&lt;/name>
35  * &lt;tagclass>org.apache.taglibs.dbtags.resultset.GetStringTag&lt;/tagclass>
36  * &lt;bodycontent>empty&lt;/bodycontent>
37  * &lt;info>Gets the value, as a String, of a coulmn in the enclosing
38  * resultset. Either set the column number via the "position" attribute,
39  * or set the column name with the "colName" attribute.
40  * You can optionally set the value, as a String, to a serlvet attribute
41  * instead of the tag body with the "to" attribute. The scope of the servlet
42  * attribute is specified by the "scope" XML attribute (default = page). Dates,
43  * times, timestamps and numbers are output according to the JVM's defaults.&lt;/info>
44  * &lt;attribute>
45  * &lt;name>position&lt;/name>
46  * &lt;required>false&lt;/required>
47  * &lt;rtexprvalue>false&lt;/rtexprvalue>
48  * &lt;/attribute>
49  * &lt;attribute>
50  * &lt;name>colName&lt;/name>
51  * &lt;required>false&lt;/required>
52  * &lt;rtexprvalue>false&lt;/rtexprvalue>
53  * &lt;/attribute>
54  * &lt;attribute>
55  * &lt;name>to&lt;/name>
56  * &lt;required>false&lt;/required>
57  * &lt;rtexprvalue>false&lt;/rtexprvalue>
58  * &lt;/attribute>
59  * &lt;attribute>
60  * &lt;name>scope&lt;/name>
61  * &lt;required>false&lt;/required>
62  * &lt;rtexprvalue>false&lt;/rtexprvalue>
63  * &lt;/attribute>
64  * </pre>
65  *
66  * @author Morgan Delagrange
67  */

68 public class GetColumnTag extends BaseGetterTag {
69     
70   public int doStartTag() throws JspTagException JavaDoc {
71     try {
72       int position = getPosition();
73       
74       ResultSet JavaDoc rset = getResultSet();
75       
76       // some complex datatypes, such as clobs,
77
// require special handling
78
ResultSetMetaData JavaDoc meta = getMetaData();
79       String JavaDoc string = null;
80       switch (meta.getColumnType(position)) {
81       case (Types.CLOB):
82         try {
83           string = readClob(rset.getClob(position));
84         } catch (IOException JavaDoc e) {
85           throw new JspTagException JavaDoc(e.toString());
86         } catch (SQLException JavaDoc e) {
87           throw new JspTagException JavaDoc(e.toString());
88         }
89         break;
90       default:
91         string = rset.getString(position);
92       }
93       
94       // null results are often OK, in outer joins for example
95
if (string == null) {
96         return EVAL_BODY_INCLUDE;
97       }
98
99       if (_attributeName != null) {
100         setAttribute(_attributeName, string, _scope);
101       } else {
102         pageContext.getOut().write(string);
103       }
104     } catch (SQLException JavaDoc e) {
105       throw new JspTagException JavaDoc(e.toString());
106     } catch (IOException JavaDoc e) {
107       throw new JspTagException JavaDoc(e.toString());
108     }
109     
110     return EVAL_BODY_INCLUDE;
111   }
112
113   private String JavaDoc readClob(Clob JavaDoc clob) throws IOException JavaDoc, SQLException JavaDoc {
114     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
115     Reader JavaDoc reader = clob.getCharacterStream();
116     BufferedReader JavaDoc buffReader = new BufferedReader JavaDoc(reader);
117     String JavaDoc line = buffReader.readLine();
118     while (line != null) {
119       buffer.append(line);
120       line = buffReader.readLine();
121     }
122     buffReader.close();
123     return buffer.toString();
124   }
125 }
126
Popular Tags