KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > 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.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 JavaDoc;
36 import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
37 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
38 import javax.sql.DataSource JavaDoc;
39 import java.sql.Connection JavaDoc;
40 import java.sql.PreparedStatement JavaDoc;
41 import java.sql.ResultSet JavaDoc;
42 import java.sql.Statement JavaDoc;
43 import java.sql.Types JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 public class SqlUpdateTag extends BodyTagSupport JavaDoc implements SQLExecutionTag {
49   private static final Logger JavaDoc log = Log.open(SqlUpdateTag.class);
50   private static final L10N L = new L10N(SqlUpdateTag.class);
51   
52   private String JavaDoc _sql;
53   private String JavaDoc _var;
54   private String JavaDoc _scope;
55   private Object JavaDoc _dataSource;
56
57   private ArrayList JavaDoc<Object JavaDoc> _params;
58
59   /**
60    * Sets the SQL.
61    */

62   public void setSql(String JavaDoc sql)
63   {
64     _sql = sql;
65   }
66
67   /**
68    * Sets the variable name.
69    */

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

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

86   public void setDataSource(Object JavaDoc dataSource)
87   {
88     _dataSource = dataSource;
89   }
90
91   /**
92    * Adds a parameter.
93    */

94   public void addSQLParameter(Object JavaDoc value)
95   {
96     if (_params == null)
97       _params = new ArrayList JavaDoc<Object JavaDoc>();
98     
99     _params.add(value);
100   }
101
102   public int doEndTag() throws JspException JavaDoc
103   {
104     Connection JavaDoc conn = null;
105     boolean isTransaction = false;
106     PageContextImpl pageContext = (PageContextImpl) this.pageContext;
107     
108     try {
109       String JavaDoc sql;
110
111       if (_sql != null)
112         sql = _sql;
113       else
114         sql = bodyContent.getString();
115
116       conn = (Connection JavaDoc) pageContext.getAttribute("caucho.jstl.sql.conn");
117       if (conn != null)
118         isTransaction = true;
119
120       if (! isTransaction) {
121         DataSource JavaDoc 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 JavaDoc value = null;
132
133       ResultSet JavaDoc rs;
134
135       ArrayList JavaDoc params = _params;
136       _params = null;
137       Statement JavaDoc 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 JavaDoc pstmt = conn.prepareStatement(sql);
147         stmt = pstmt;
148
149         for (int i = 0; i < params.size(); i++) {
150           Object JavaDoc 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 JavaDoc(rows));
164     } catch (Exception JavaDoc e) {
165       throw new JspException JavaDoc(e);
166     } finally {
167       if (! isTransaction && conn != null) {
168         try {
169           conn.close();
170         } catch (Exception JavaDoc e) {
171           log.log(Level.FINE, e.toString(), e);
172         }
173       }
174     }
175
176     return EVAL_PAGE;
177   }
178 }
179
Popular Tags