?? getcolumntag.java
字號:
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.taglibs.dbtags.resultset;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import javax.servlet.jsp.JspTagException;
/**
* <p>Get the value of a database column as a String.</p>
*
* <p>JSP Tag Lib Descriptor
* <pre>
* <name>getString</name>
* <tagclass>org.apache.taglibs.dbtags.resultset.GetStringTag</tagclass>
* <bodycontent>empty</bodycontent>
* <info>Gets the value, as a String, of a coulmn in the enclosing
* resultset. Either set the column number via the "position" attribute,
* or set the column name with the "colName" attribute.
* You can optionally set the value, as a String, to a serlvet attribute
* instead of the tag body with the "to" attribute. The scope of the servlet
* attribute is specified by the "scope" XML attribute (default = page). Dates,
* times, timestamps and numbers are output according to the JVM's defaults.</info>
* <attribute>
* <name>position</name>
* <required>false</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* <attribute>
* <name>colName</name>
* <required>false</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* <attribute>
* <name>to</name>
* <required>false</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* <attribute>
* <name>scope</name>
* <required>false</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* </pre>
*
* @author Morgan Delagrange
*/
public class GetColumnTag extends BaseGetterTag {
public int doStartTag() throws JspTagException {
try {
int position = getPosition();
ResultSet rset = getResultSet();
// some complex datatypes, such as clobs,
// require special handling
ResultSetMetaData meta = getMetaData();
String string = null;
switch (meta.getColumnType(position)) {
case (Types.CLOB):
try {
string = readClob(rset.getClob(position));
} catch (IOException e) {
throw new JspTagException(e.toString());
} catch (SQLException e) {
throw new JspTagException(e.toString());
}
break;
default:
string = rset.getString(position);
}
// null results are often OK, in outer joins for example
if (string == null) {
return EVAL_BODY_INCLUDE;
}
if (_attributeName != null) {
setAttribute(_attributeName, string, _scope);
} else {
pageContext.getOut().write(string);
}
} catch (SQLException e) {
throw new JspTagException(e.toString());
} catch (IOException e) {
throw new JspTagException(e.toString());
}
return EVAL_BODY_INCLUDE;
}
private String readClob(Clob clob) throws IOException, SQLException {
StringBuffer buffer = new StringBuffer();
Reader reader = clob.getCharacterStream();
BufferedReader buffReader = new BufferedReader(reader);
String line = buffReader.readLine();
while (line != null) {
buffer.append(line);
line = buffReader.readLine();
}
buffReader.close();
return buffer.toString();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -