1 28 29 package com.caucho.jstl.rt; 30 31 import com.caucho.jsp.PageContextImpl; 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 35 import javax.servlet.jsp.JspException ; 36 import javax.servlet.jsp.jstl.sql.SQLExecutionTag; 37 import javax.servlet.jsp.tagext.BodyTagSupport ; 38 import javax.sql.DataSource ; 39 import java.sql.Connection ; 40 import java.sql.PreparedStatement ; 41 import java.sql.ResultSet ; 42 import java.sql.Statement ; 43 import java.sql.Types ; 44 import java.util.ArrayList ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 public class SqlUpdateTag extends BodyTagSupport implements SQLExecutionTag { 49 private static final Logger log = Log.open(SqlUpdateTag.class); 50 private static final L10N L = new L10N(SqlUpdateTag.class); 51 52 private String _sql; 53 private String _var; 54 private String _scope; 55 private Object _dataSource; 56 57 private ArrayList <Object > _params; 58 59 62 public void setSql(String sql) 63 { 64 _sql = sql; 65 } 66 67 70 public void setVar(String var) 71 { 72 _var = var; 73 } 74 75 78 public void setScope(String scope) 79 { 80 _scope = scope; 81 } 82 83 86 public void setDataSource(Object dataSource) 87 { 88 _dataSource = dataSource; 89 } 90 91 94 public void addSQLParameter(Object value) 95 { 96 if (_params == null) 97 _params = new ArrayList <Object >(); 98 99 _params.add(value); 100 } 101 102 public int doEndTag() throws JspException 103 { 104 Connection conn = null; 105 boolean isTransaction = false; 106 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 107 108 try { 109 String sql; 110 111 if (_sql != null) 112 sql = _sql; 113 else 114 sql = bodyContent.getString(); 115 116 conn = (Connection ) pageContext.getAttribute("caucho.jstl.sql.conn"); 117 if (conn != null) 118 isTransaction = true; 119 120 if (! isTransaction) { 121 DataSource ds; 122 123 if (_dataSource != null) 124 ds = SqlQueryTag.getDataSource(pageContext, _dataSource); 125 else 126 ds = SqlQueryTag.getDataSource(pageContext, null); 127 128 conn = ds.getConnection(); 129 } 130 131 Object value = null; 132 133 ResultSet rs; 134 135 ArrayList params = _params; 136 _params = null; 137 Statement stmt; 138 139 int rows = 0; 140 141 if (params == null) { 142 stmt = conn.createStatement(); 143 rows = stmt.executeUpdate(sql); 144 } 145 else { 146 PreparedStatement pstmt = conn.prepareStatement(sql); 147 stmt = pstmt; 148 149 for (int i = 0; i < params.size(); i++) { 150 Object paramValue = params.get(i); 151 152 if (paramValue == null) 153 pstmt.setNull(i + 1, Types.VARCHAR); 154 else 155 pstmt.setObject(i + 1, paramValue); 156 } 157 158 rows = pstmt.executeUpdate(); 159 } 160 161 stmt.close(); 162 163 CoreSetTag.setValue(pageContext, _var, _scope, new Integer (rows)); 164 } catch (Exception e) { 165 throw new JspException (e); 166 } finally { 167 if (! isTransaction && conn != null) { 168 try { 169 conn.close(); 170 } catch (Exception e) { 171 log.log(Level.FINE, e.toString(), e); 172 } 173 } 174 } 175 176 return EVAL_PAGE; 177 } 178 } 179 | Popular Tags |