KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > SqlUpdateTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl.el;
30
31 import com.caucho.el.Expr;
32 import com.caucho.jsp.PageContextImpl;
33 import com.caucho.log.Log;
34 import com.caucho.util.L10N;
35
36 import javax.el.ELContext;
37 import javax.servlet.jsp.JspException JavaDoc;
38 import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
39 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
40 import javax.sql.DataSource JavaDoc;
41 import java.sql.Connection JavaDoc;
42 import java.sql.PreparedStatement JavaDoc;
43 import java.sql.ResultSet JavaDoc;
44 import java.sql.Statement JavaDoc;
45 import java.sql.Types JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 public class SqlUpdateTag extends BodyTagSupport JavaDoc implements SQLExecutionTag {
51   private static final Logger JavaDoc log = Log.open(SqlUpdateTag.class);
52   private static final L10N L = new L10N(SqlUpdateTag.class);
53   
54   private Expr _sql;
55   private String JavaDoc _var;
56   private String JavaDoc _scope;
57   private Expr _dataSource;
58
59   private ArrayList JavaDoc<Object JavaDoc> _params;
60
61   /**
62    * Sets the JSP-EL expression for the SQL.
63    */

64   public void setSql(Expr sql)
65   {
66     _sql = sql;
67   }
68
69   /**
70    * Sets the variable name.
71    */

72   public void setVar(String JavaDoc var)
73   {
74     _var = var;
75   }
76
77   /**
78    * Sets the scope.
79    */

80   public void setScope(String JavaDoc scope)
81   {
82     _scope = scope;
83   }
84
85   /**
86    * Sets the data source.
87    */

88   public void setDataSource(Expr dataSource)
89   {
90     _dataSource = dataSource;
91   }
92
93   /**
94    * Adds a parameter.
95    */

96   public void addSQLParameter(Object JavaDoc value)
97   {
98     if (_params == null)
99       _params = new ArrayList JavaDoc<Object JavaDoc>();
100     
101     _params.add(value);
102   }
103
104   public int doEndTag() throws JspException JavaDoc
105   {
106     Connection JavaDoc conn = null;
107     boolean isTransaction = false;
108     PageContextImpl pageContext = (PageContextImpl) this.pageContext;
109     ELContext env = pageContext.getELContext();
110     
111     try {
112       String JavaDoc sql;
113
114       if (_sql != null)
115         sql = _sql.evalString(env);
116       else
117         sql = bodyContent.getString();
118
119       conn = (Connection JavaDoc) pageContext.getAttribute("caucho.jstl.sql.conn");
120       if (conn != null)
121         isTransaction = true;
122
123       if (! isTransaction) {
124         DataSource JavaDoc ds;
125
126         if (_dataSource != null)
127           ds = SqlQueryTag.getDataSource(pageContext,
128                                          _dataSource.evalObject(env));
129         else
130           ds = SqlQueryTag.getDataSource(pageContext, null);
131
132         conn = ds.getConnection();
133       }
134
135       Object JavaDoc value = null;
136
137       ResultSet JavaDoc rs;
138
139       ArrayList JavaDoc params = _params;
140       _params = null;
141       Statement JavaDoc stmt;
142
143       int rows = 0;
144
145       if (params == null) {
146         stmt = conn.createStatement();
147         rows = stmt.executeUpdate(sql);
148       }
149       else {
150         PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
151         stmt = pstmt;
152
153         for (int i = 0; i < params.size(); i++) {
154           Object JavaDoc paramValue = params.get(i);
155
156       if (paramValue == null)
157         pstmt.setNull(i + 1, Types.VARCHAR);
158       else
159         pstmt.setObject(i + 1, paramValue);
160         }
161
162         rows = pstmt.executeUpdate();
163       }
164
165       stmt.close();
166
167       CoreSetTag.setValue(pageContext, _var, _scope, new Integer JavaDoc(rows));
168     } catch (Exception JavaDoc e) {
169       throw new JspException JavaDoc(e);
170     } finally {
171       if (! isTransaction && conn != null) {
172         try {
173           conn.close();
174         } catch (Exception JavaDoc e) {
175           log.log(Level.FINE, e.toString(), e);
176         }
177       }
178     }
179
180     return EVAL_PAGE;
181   }
182 }
183
Popular Tags