KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > tables > Tosqltime


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Tosqltime.java,v 1.3 2005/05/11 15:29:07 ashah Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.tables;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.Statement JavaDoc;
32 import java.sql.Time JavaDoc;
33
34 import javax.naming.NamingException JavaDoc;
35
36 import org.objectweb.jonas.jtests.util.Env;
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39
40 /**
41  * Class to create tables for the 'osqltime' bean of 'etype'.
42  * @author Helene Joanin
43  */

44 public class Tosqltime extends Tmanager {
45
46     /**
47      * Entry point
48      */

49     public static void init() throws NamingException JavaDoc, RemoteException JavaDoc {
50         mgrInit();
51         createTable("JT_EtypeOsqltimeEC");
52     }
53
54     /**
55      * create a table for the osqltime bean of etype
56      */

57     private static void createTable(String JavaDoc name) throws RemoteException JavaDoc {
58
59         // get connection
60
Connection JavaDoc conn = null;
61         try {
62             conn = dataSource.getConnection();
63         } catch(Exception JavaDoc e) {
64             throw new RemoteException JavaDoc("Cannot get Connection");
65         }
66     
67         Statement JavaDoc stmt;
68         PreparedStatement JavaDoc pStmt;
69         try {
70             stmt = conn.createStatement();
71             stmt.execute("DROP TABLE "+name);
72             stmt.close();
73             logger.log(BasicLevel.INFO, "Table "+name+" dropped");
74         } catch(Exception JavaDoc e) {
75             logger.log(BasicLevel.DEBUG, "Exception in dropTable : \n"+e);
76         }
77         try {
78             int dbType = Env.getDatabaseType(conn);
79             String JavaDoc cTypeName;
80             switch (dbType) {
81             case Env.DB_ORACLE: cTypeName = "date"; break;
82             default: cTypeName = "time"; // standard jdbc type name
83
}
84             stmt = conn.createStatement();
85             stmt.execute("create table " + name +
86                          "( c_pk varchar(30) not null primary key, c_f1 "+cTypeName+")");
87             stmt.close();
88             pStmt = conn.prepareStatement("insert into "+name+" values('pk1', ?)");
89             pStmt.setTime(1, Time.valueOf("01:00:00"));
90             pStmt.executeUpdate();
91             pStmt.close();
92             pStmt = conn.prepareStatement("insert into "+name+" values('pk2', ?)");
93             pStmt.setTime(1, Time.valueOf("02:00:00"));
94             pStmt.executeUpdate();
95             pStmt.close();
96             pStmt = conn.prepareStatement("insert into "+name+" values('pk3', ?)");
97             pStmt.setTime(1, Time.valueOf("03:00:00"));
98             pStmt.executeUpdate();
99             pStmt.close();
100             pStmt = conn.prepareStatement("insert into "+name+" values('pk4', ?)");
101             pStmt.setTime(1, Time.valueOf("04:00:00"));
102             pStmt.executeUpdate();
103             pStmt.close();
104             pStmt = conn.prepareStatement("insert into "+name+" values('pk5', ?)");
105             pStmt.setTime(1, Time.valueOf("05:00:00"));
106             pStmt.executeUpdate();
107             pStmt.close();
108             pStmt = conn.prepareStatement("insert into "+name+" values('pk5bis', ?)");
109             pStmt.setTime(1, Time.valueOf("05:00:00"));
110             pStmt.executeUpdate();
111             pStmt.close();
112             pStmt = conn.prepareStatement("insert into "+name+" values('pktoremove', ?)");
113             pStmt.setTime(1, Time.valueOf("12:00:00"));
114             pStmt.executeUpdate();
115             pStmt.close();
116             pStmt = conn.prepareStatement("insert into "+name+" values('pknull', NULL)");
117             pStmt.executeUpdate();
118             pStmt.close();
119             pStmt = conn.prepareStatement("insert into "+name+" values('pkchangenull', ?)");
120             pStmt.setTime(1, Time.valueOf("12:00:00"));
121             pStmt.executeUpdate();
122             pStmt.close();
123             conn.close(); // release connection
124
} catch(Exception JavaDoc e) {
125             logger.log(BasicLevel.ERROR, "Exception in createTable : "+e);
126             throw new RemoteException JavaDoc("Exception in createTable : "+e);
127         }
128         logger.log(BasicLevel.INFO, "Table "+name+" created");
129     }
130
131 }
132
Popular Tags