KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > raidb1 > driver > UpdatableResultSetScenario


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Jean-Bernard van Zuylen.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.raidb1.driver;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.Statement JavaDoc;
30
31 import org.objectweb.cjdbc.scenario.templates.Raidb1Template;
32
33 /**
34  * This class defines a UpdatableResultSetScenario
35  *
36  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen </a>
37  * @version 1.0
38  */

39 public class UpdatableResultSetScenario extends Raidb1Template
40 {
41
42   String JavaDoc tableName;
43   
44   /**
45    * @see junit.framework.TestCase#setUp()
46    */

47   protected void setUp()
48   {
49     tableName = ("test" + System.currentTimeMillis()).toUpperCase();
50     super.setUp();
51     
52     // Create test table
53
try
54     {
55       Connection JavaDoc con = getCJDBCConnection();
56       Statement JavaDoc stmt = con.createStatement();
57       stmt.executeUpdate("CREATE TABLE " + tableName + "("
58           + "ACCOUNT VARCHAR(20) NOT NULL, AMOUNT INTEGER, BLOCKED BOOLEAN, "
59           + "PRIMARY KEY(ACCOUNT))");
60       stmt.close();
61       con.close();
62     }
63     catch (Exception JavaDoc e)
64     {
65     }
66   }
67   
68   /**
69    * @see junit.framework.TestCase#tearDown()
70    */

71   protected void tearDown()
72   {
73     // Drop test table
74
try
75     {
76       Connection JavaDoc con = getCJDBCConnection();
77       Statement JavaDoc stmt = con.createStatement();
78       stmt.executeUpdate("DROP TABLE " + tableName);
79       stmt.close();
80       con.close();
81     }
82     catch (Exception JavaDoc e)
83     {
84     }
85     
86     super.tearDown();
87   }
88   
89   /**
90    * Test inserting rows with Updatable ResultSets
91    *
92    * @throws Exception if fails
93    */

94   public void testInsertRow() throws Exception JavaDoc
95   {
96     Connection JavaDoc con = getCJDBCConnection();
97     Statement JavaDoc stmt = con.createStatement(
98         ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
99     ResultSet JavaDoc res = stmt.executeQuery("SELECT * FROM " + tableName);
100
101     // insert a new row
102
res.moveToInsertRow();
103     
104     assertNull(res.getString(1));
105     res.updateString(1, "111-2222222-33");
106     assertEquals(res.getString(1), "111-2222222-33");
107     
108     assertEquals(res.getInt(2), 0);
109     res.updateInt(2, 1000);
110     assertEquals(res.getInt(2), 1000);
111     
112     assertFalse(res.getBoolean(3));
113     res.updateBoolean(3, true);
114     assertTrue(res.getBoolean(3));
115     
116     res.insertRow();
117     
118     // check row added to resultset
119
res.next();
120     assertEquals(res.getString(1), "111-2222222-33");
121     assertEquals(res.getInt(2), 1000);
122     assertTrue(res.getBoolean(3));
123     res.close();
124     
125     // check row added to database
126
res = stmt.executeQuery("SELECT * FROM " + tableName);
127     res.next();
128     assertEquals(res.getString(1), "111-2222222-33");
129     assertEquals(res.getInt(2), 1000);
130     //assertTrue(res.getBoolean(3));
131
res.close();
132     
133     // Clean up
134
stmt.close();
135     con.close();
136   }
137   
138   /**
139    * Test deleting rows with updatable ResultSets
140    *
141    * @throws Exception if fails
142    */

143   public void testDeleteRow() throws Exception JavaDoc
144   {
145     Connection JavaDoc con = getCJDBCConnection();
146     Statement JavaDoc stmt = con.createStatement();
147     stmt.executeUpdate("INSERT INTO " + tableName
148         + " VALUES ('111-2222222-33', 1000, TRUE)");
149     stmt.close();
150     
151     stmt = con.createStatement(
152         ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
153     ResultSet JavaDoc res = stmt.executeQuery("SELECT * FROM " + tableName);
154
155     // delete row in resultset
156
res.next();
157     res.deleteRow();
158     
159     // check row removed from resultset
160
assertFalse(res.next());
161     res.close();
162     
163     // check row added from database
164
res = stmt.executeQuery("SELECT * FROM " + tableName);
165     assertFalse(res.next());
166     
167     // clean up
168
stmt.close();
169     con.close();
170   }
171   
172   /**
173    * Test updating rows with Updatable ResultSets
174    *
175    * @throws Exception if fails
176    */

177   public void testUpdateRow() throws Exception JavaDoc
178   {
179     Connection JavaDoc con = getCJDBCConnection();
180     Statement JavaDoc stmt = con.createStatement();
181     stmt.executeUpdate("INSERT INTO " + tableName
182         + " VALUES ('111-2222222-33', 1000, TRUE)");
183     stmt.close();
184     
185     stmt = con.createStatement(
186         ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
187     ResultSet JavaDoc res = stmt.executeQuery("SELECT * FROM " + tableName);
188
189     // update row
190
res.next();
191     
192     assertEquals(res.getString(1), "111-2222222-33");
193     res.updateString(1, "777-8888888-99");
194     assertEquals(res.getString(1), "777-8888888-99");
195     
196     assertEquals(res.getInt(2), 1000);
197     res.updateInt(2, 2000);
198     assertEquals(res.getInt(2), 2000);
199     
200     assertTrue(res.getBoolean(3));
201     res.updateBoolean(3, false);
202     assertFalse(res.getBoolean(3));
203     
204     res.updateRow();
205
206     // check row updated in resultset
207
res.first();
208     assertEquals(res.getString(1), "777-8888888-99");
209     assertEquals(res.getInt(2), 2000);
210     assertFalse(res.getBoolean(3));
211     res.close();
212     
213     // check row added to database
214
res = stmt.executeQuery("SELECT * FROM " + tableName);
215     res.next();
216     assertEquals(res.getString(1), "777-8888888-99");
217     assertEquals(res.getInt(2), 2000);
218     assertFalse(res.getBoolean(3));
219     res.close();
220     
221     // Clean up
222
stmt.close();
223     con.close();
224   }
225   
226   /**
227    * Test refreshing rows with Updatable ResultSets
228    *
229    * @throws Exception if fails
230    */

231   public void testRefreshRow() throws Exception JavaDoc
232   {
233     Connection JavaDoc con = getCJDBCConnection();
234     Statement JavaDoc stmt = con.createStatement();
235     stmt.executeUpdate("INSERT INTO " + tableName
236         + " VALUES ('111-2222222-33', 1000, TRUE)");
237     
238     Statement JavaDoc stmt2 = con.createStatement(
239         ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
240     ResultSet JavaDoc res = stmt2.executeQuery("select * from " + tableName);
241     
242     // update row in database
243
stmt.executeUpdate("UPDATE " + tableName + " SET AMOUNT = 2000,"
244         + " BLOCKED = FALSE WHERE ACCOUNT = '111-2222222-33'");
245
246     // refresh row in resultset
247
res.next();
248     res.refreshRow();
249     assertEquals(res.getString(1), "111-2222222-33");
250     assertEquals(res.getInt(2), 2000);
251     assertFalse(res.getBoolean(3));
252     res.close();
253     
254     // clean up
255
stmt2.close();
256     stmt.close();
257     con.close();
258   }
259 }
260
Popular Tags