KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > UpdateTag


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.jsp.tag;
37
38 import java.util.*;
39 import java.util.regex.*;
40 import java.sql.*;
41 import javax.sql.*;
42 import javax.naming.*;
43 import javax.servlet.jsp.*;
44 import javax.servlet.jsp.jstl.core.Config;
45 import org.apache.taglibs.standard.tag.common.sql.ResultImpl;
46 import com.micronova.util.*;
47
48 /** templated update statement **/
49
50 public class UpdateTag extends EvalTag
51 {
52     protected Object JavaDoc _dataSource;
53     protected List _nameList;
54     protected StringBuffer JavaDoc _buffer;
55
56     protected void init()
57     {
58         super.init();
59
60         _pattern = EL.DEFAULTPATTERNQUERY;
61         _dataSource = null;
62         _export = null;
63
64         _nameList = null;
65
66         if (_buffer == null)
67         {
68             _buffer = new StringBuffer JavaDoc();
69         }
70     }
71
72     protected Object JavaDoc doSql(PreparedStatement preparedStatement) throws Exception JavaDoc
73     {
74         return new Integer JavaDoc(preparedStatement.executeUpdate());
75     }
76
77     protected Connection getConnection(Object JavaDoc spec) throws Exception JavaDoc
78     {
79         if (spec == null)
80         {
81             spec = getConfiguration(Config.SQL_DATA_SOURCE, null);
82         }
83
84         return TransactionTag.openConnection(spec);
85     }
86
87     protected Object JavaDoc evaluate(Object JavaDoc tagValue, Pattern pattern) throws Exception JavaDoc
88     {
89         Object JavaDoc value = null;
90         String JavaDoc expression = tagValue.toString();
91
92         Connection connection = null;
93         boolean doesCloseConnection = false;
94         PreparedStatement preparedStatement = null;
95
96         try
97         {
98             Object JavaDoc dataSource = _dataSource;
99
100             if (dataSource == null)
101             {
102                 connection = TransactionTag.getAncestorConnection(this);
103             }
104
105             if (connection == null)
106             {
107                 doesCloseConnection = true;
108                
109                 connection = getConnection(dataSource);
110             }
111
112             preparedStatement = SQLObjectSource.prepareStatement(connection, expression, pattern, this, _recursive, _allowGroup);
113             
114             value = doSql(preparedStatement);
115         }
116         catch (Exception JavaDoc e)
117         {
118             throw e;
119         }
120         finally
121         {
122             if (doesCloseConnection)
123             {
124                 if (preparedStatement != null)
125                 {
126                     preparedStatement.close();
127                 }
128
129                 if (connection != null)
130                 {
131                     connection.close();
132                 }
133             }
134         }
135
136         return value;
137     }
138
139     public void setDataSource(Object JavaDoc expression) throws Exception JavaDoc
140     {
141         Object JavaDoc dataSource = evaluateAttribute("dataSource", expression, Object JavaDoc.class);
142
143         if (!isEmptyString(dataSource))
144         {
145             _dataSource = dataSource;
146         }
147     }
148 }
149
Popular Tags