1 16 package org.apache.taglibs.dbtags.resultset; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.io.Reader ; 21 import java.sql.Clob ; 22 import java.sql.ResultSet ; 23 import java.sql.ResultSetMetaData ; 24 import java.sql.SQLException ; 25 import java.sql.Types ; 26 27 import javax.servlet.jsp.JspTagException ; 28 29 68 public class GetColumnTag extends BaseGetterTag { 69 70 public int doStartTag() throws JspTagException { 71 try { 72 int position = getPosition(); 73 74 ResultSet rset = getResultSet(); 75 76 ResultSetMetaData meta = getMetaData(); 79 String string = null; 80 switch (meta.getColumnType(position)) { 81 case (Types.CLOB): 82 try { 83 string = readClob(rset.getClob(position)); 84 } catch (IOException e) { 85 throw new JspTagException (e.toString()); 86 } catch (SQLException e) { 87 throw new JspTagException (e.toString()); 88 } 89 break; 90 default: 91 string = rset.getString(position); 92 } 93 94 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 e) { 105 throw new JspTagException (e.toString()); 106 } catch (IOException e) { 107 throw new JspTagException (e.toString()); 108 } 109 110 return EVAL_BODY_INCLUDE; 111 } 112 113 private String readClob(Clob clob) throws IOException , SQLException { 114 StringBuffer buffer = new StringBuffer (); 115 Reader reader = clob.getCharacterStream(); 116 BufferedReader buffReader = new BufferedReader (reader); 117 String 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 |