1 /***
2 * Copyright 2004 Steven Caswell
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.mungoknotwise.sqlcli;
17
18 import java.sql.Connection;
19 import java.sql.Statement;
20 import org.apache.log4j.Logger;
21
22 /***
23 * Performs a non-SELECT SQL command and handles the resulting rowcount.
24 *
25 * @author Steven Caswell
26 * @version $Id: UpdateCommand.java,v 1.2 2004/07/28 23:26:36 mungoknotwise Exp $
27 */
28 public class UpdateCommand
29 implements RowcountHandlingCommand
30 {
31
32
33
34
35
36 private static Logger logger = Logger.getLogger(UpdateCommand.class);
37
38
39
40
41
42
43
44
45
46
47
48
49
50 private String command;
51 private Connection connection;
52 private ExceptionHandler exceptionHandler;
53 private UpdateRowcountHandler rowcountHandler;
54
55
56
57
58
59 /***
60 * Constructs a new instance of
61 * <code>UpdateCommand</code>.
62 */
63 public UpdateCommand()
64 {
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 /***
87 * Executes the command, handling the results.
88 */
89 public void execute()
90 {
91 try
92 {
93 Statement statement = this.connection.createStatement();
94 int rowCount = statement.executeUpdate(this.command);
95 this.rowcountHandler.handleRowcount(rowCount);
96 }
97 catch(Exception e)
98 {
99 this.exceptionHandler.handleException(e);
100 }
101 }
102
103 /***
104 * Sets the SQL command to be executed.
105 *
106 * @param command the command
107 */
108 public void setCommand(final String command)
109 {
110 this.command = command;
111 }
112
113 /***
114 * Sets the connection for this command.
115 *
116 * @param connection the connection
117 */
118 public void setConnection(final Connection connection)
119 {
120 this.connection = connection;
121 }
122
123 /***
124 * {@inheritDoc}
125 */
126 public void setExceptionHandler(final ExceptionHandler exceptionHandler)
127 {
128 this.exceptionHandler = exceptionHandler;
129 }
130
131 /***
132 * {@inheritDoc}
133 */
134 public void setRowcountHandler(final UpdateRowcountHandler rowcountHandler)
135 {
136 this.rowcountHandler = rowcountHandler;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 }