KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > testlet > ColumnUpdateTestLet


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 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): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.testlet;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29
30 /**
31  * Same as the <tt>executeUpdateLet</tt>, except the update is done on a
32  * specific column
33  *
34  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
35  * @version 1.0
36  */

37 public class ColumnUpdateTestLet extends AbstractConnectionTestLet
38 {
39
40   /**
41    * Creates a new <code>ColumnUpdateTestLet</code> object
42    *
43    * @param con connection to use for testing
44    */

45   public ColumnUpdateTestLet(Connection JavaDoc con)
46   {
47     super(con);
48     config.put(TABLE_NAME, "ADDRESS");
49     config.put(COLUMN_NAME, "ID");
50     config.put(UPDATED_COLUMN_VALUE, "emmanuel");
51     config.put(SELECTED_COLUMNS, "FIRSTNAME");
52     config.put(USE_PREPARED_STATEMENT, "true");
53     config.put(USE_TRANSACTIONS,"false");
54   }
55
56   /**
57    * @see org.objectweb.cjdbc.scenario.tools.testlet.AbstractTestLet#execute()
58    */

59   public void execute() throws Exception JavaDoc
60   {
61     String JavaDoc selectQuery = "select " + config.get(SELECTED_COLUMNS) + " from "
62         + config.get(TABLE_NAME) + " where " + config.get(COLUMN_NAME) + "=0";
63     String JavaDoc updateQuery = "update " + config.get(TABLE_NAME) + " set "
64         + config.get(SELECTED_COLUMNS) + "='"
65         + config.get(UPDATED_COLUMN_VALUE) + "' where "
66         + config.get(COLUMN_NAME) + "=0";
67     ResultSet JavaDoc rs1, rs2;
68
69
70     if (usePreparedStatement())
71     {
72       if(useTransaction())
73         jdbcConnection.setAutoCommit(false);
74       rs1 = jdbcConnection.createStatement().executeQuery(selectQuery);
75       jdbcConnection.createStatement().executeUpdate(updateQuery);
76       rs2 = jdbcConnection.createStatement().executeQuery(selectQuery);
77       if(useTransaction())
78         jdbcConnection.commit();
79     }
80     else
81     {
82       if(useTransaction())
83         jdbcConnection.setAutoCommit(false);
84       rs1 = jdbcConnection.prepareStatement(selectQuery).executeQuery();
85       jdbcConnection.createStatement().executeUpdate(updateQuery);
86       rs2 = jdbcConnection.prepareStatement(selectQuery).executeQuery();
87       if(useTransaction())
88         jdbcConnection.commit();
89     }
90
91     assertTrue("No results", rs1.next());
92     String JavaDoc laura = rs1.getString((String JavaDoc) config.get(SELECTED_COLUMNS));
93     assertEquals("Was expecting Laura", laura, "Laura");
94
95     assertTrue("No results", rs2.next());
96     String JavaDoc emmanuel = rs2.getString((String JavaDoc) config.get(SELECTED_COLUMNS));
97     System.out.println(emmanuel);
98     assertEquals("Was expecting Emmanuel", emmanuel, (String JavaDoc) config.get(UPDATED_COLUMN_VALUE));
99   }
100
101 }
Popular Tags