1 16 package org.apache.taglibs.dbtags.preparedstatement; 17 18 import java.io.IOException ; 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 24 import javax.servlet.jsp.JspTagException ; 25 import javax.servlet.jsp.tagext.BodyTagSupport ; 26 27 import org.apache.taglibs.dbtags.statement.StatementTag; 28 29 59 public class PreparedStatementImplTag extends BodyTagSupport 60 implements StatementTag { 61 62 private String _connId = null; 63 private PreparedStatement _statement = null; 64 65 public void setQuery(String query) throws SQLException , JspTagException { 66 Connection conn = (Connection )pageContext.findAttribute(_connId); 67 if(conn == null) { 68 throw new JspTagException ("There is no such connection'"+_connId+"'"); 69 } 70 _statement = createStatement ( conn, query ); 71 pageContext.setAttribute(getId(), _statement); 72 } 73 74 77 protected PreparedStatement createStatement ( Connection theConnection, String theQuery ) throws SQLException 78 { 79 return theConnection.prepareStatement ( theQuery ); 80 } 81 82 83 87 public void setConn(String connId) { 88 _connId = connId; 89 } 90 91 96 public PreparedStatement getPreparedStatement() { 97 return _statement; 98 } 99 100 public void executeUpdate() throws SQLException { 101 _statement.executeUpdate(); 102 } 103 104 public ResultSet executeQuery() throws SQLException { 105 return _statement.executeQuery(); 106 } 107 108 public int doStartTag() throws JspTagException { 109 if (_connId == null) { 110 throw new JspTagException ("Connection id has not been set."); 111 } 112 113 117 return EVAL_BODY_TAG; 118 } 119 120 public int doAfterBody() throws JspTagException 121 { 122 try { 124 getPreviousOut().write(getBodyContent().getString().trim()); 125 } catch (IOException e) { 126 throw new JspTagException (e.toString()); 127 } 128 return EVAL_PAGE; 129 } 130 131 public int doEndTag() { 132 pageContext.removeAttribute(getId()); 133 134 try { 135 _statement.close(); 136 } catch (SQLException e) { 137 e.printStackTrace(); 139 } 140 141 return EVAL_PAGE; 142 } 143 144 public void release() { 145 _connId = null; 146 _statement = null; 147 } 148 } 149 | Popular Tags |