1 16 package org.apache.taglibs.dbtags.statement; 17 18 import java.io.IOException ; 19 import java.sql.Connection ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.sql.Statement ; 23 24 import javax.servlet.jsp.JspTagException ; 25 import javax.servlet.jsp.tagext.BodyTagSupport ; 26 27 54 public class StatementImplTag extends BodyTagSupport implements StatementTag{ 55 56 private Statement _statement = null; 57 private String _query = null; 58 private String _connId = null; 59 60 63 public void setQuery(String query) { 64 _query = query; 65 } 66 67 public void setConn(String connId) { 68 _connId = connId; 69 } 70 71 public void executeUpdate() throws SQLException { 72 _statement.executeUpdate(_query); 73 } 74 75 public ResultSet executeQuery() throws SQLException { 76 return _statement.executeQuery(_query); 77 } 78 79 public int doStartTag() throws JspTagException { 80 if (_connId == null) { 81 throw new JspTagException ("Connection id has not been set."); 82 } 83 84 try { 85 Connection conn = (Connection )pageContext.findAttribute(_connId); 86 if(conn == null) { 87 throw new JspTagException ("There is no such connection'"+_connId+"'"); 88 } 89 _statement = createStatement ( conn ); 90 pageContext.setAttribute(getId(), _statement); 91 } catch (SQLException e) { 92 throw new JspTagException (e.toString()); 93 } 94 95 return EVAL_BODY_TAG; 96 } 97 98 99 102 protected Statement createStatement ( Connection theConnection ) throws SQLException 103 { 104 return theConnection.createStatement(); 105 } 106 107 108 public int doAfterBody() throws JspTagException 109 { 110 try { 112 getPreviousOut().write(getBodyContent().getString().trim()); 113 } catch (IOException e) { 114 throw new JspTagException (e.toString()); 115 } 116 return EVAL_PAGE; 117 } 118 119 public int doEndTag() { 120 pageContext.removeAttribute(getId()); 121 122 try { 123 _statement.close(); 124 } catch (SQLException e) { 125 e.printStackTrace(); 127 } 128 129 return EVAL_PAGE; 130 } 131 132 public void release() { 133 _connId=null; 134 _statement = null; 135 _query=null; 136 } 137 138 } 139 | Popular Tags |