KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foo > DBTest


1 /*
2  * @(#) DBTest.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  *
7  * This module was orginally developed by
8  *
9  * - INRIA (www.inria.fr)inside the ObjectWeb Consortium
10  * (http://www.objectweb.org)
11  *
12  * --------------------------------------------------------------------------
13  * The original code and portions created by INRIA are
14  * Copyright (c) 2002 INRIA
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * -Redistributions of source code must retain the above copyright notice, this
21  * list of conditions and the following disclaimer.
22  *
23  * -Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * --------------------------------------------------------------------------
40  * $Id: DBTest.java,v 1.2 2003/12/05 19:03:56 trentshue Exp $
41  * --------------------------------------------------------------------------
42  */

43  
44  package foo;
45
46 import java.sql.PreparedStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48 import java.sql.Statement JavaDoc;
49
50 import javax.naming.Context JavaDoc;
51 import javax.naming.InitialContext JavaDoc;
52 import javax.sql.DataSource JavaDoc;
53 import javax.transaction.UserTransaction JavaDoc;
54
55 public class DBTest{
56
57     int foo = -1;
58     // value stored in DB
59

60     public void init(String JavaDoc completion) {
61         try{
62             Context JavaDoc ctx = new InitialContext JavaDoc();
63  
64             // JDBC stuff
65
DataSource JavaDoc ds =
66                 (DataSource JavaDoc)ctx.lookup("java:comp/env/jdbc/myDB");
67
68             UserTransaction JavaDoc ut = (UserTransaction JavaDoc)ctx.lookup("java:comp/UserTransaction");
69
70             java.sql.Connection JavaDoc conn = ds.getConnection();
71
72             System.out.println("<<< beginning the transaction >>>");
73             ut.begin();
74
75              // JDBC statements
76
Statement JavaDoc stmt = conn.createStatement();
77              ResultSet JavaDoc rst =
78                  stmt.executeQuery("select id, foo from testdata");
79              if(rst.next()) {
80                  foo=rst.getInt(2);
81              }
82              System.out.println("foo = "+ foo +" (before completion)");
83
84              PreparedStatement JavaDoc pstmt = conn.prepareStatement("update testdata set foo=? where id=1");
85              pstmt.setInt(1,++foo);
86              pstmt.executeUpdate();
87
88               if (completion != null && completion.equals("commit")) {
89                   System.out.println("<<< committing the transaction >>>");
90                   ut.commit();
91               } else {
92                   System.out.println("<<< rolling back the transaction >>>");
93                   ut.rollback();
94               }
95
96              // we set foo to the value stored in the DB
97
rst =
98                  stmt.executeQuery("select id, foo from testdata");
99              if(rst.next()) {
100                  foo=rst.getInt(2);
101              }
102              System.out.println("foo = "+ foo +" (after completion)");
103
104              conn.close();
105              System.out.println("<<< done >>>");
106         }catch(Exception JavaDoc e) {
107             System.out.print("DBTest >> ");
108             e.printStackTrace();
109         }
110     }
111
112     public String JavaDoc getFoo() { return ""+foo; }
113 }
114
115
Popular Tags