KickJava   Java API By Example, From Geeks To Geeks.

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


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: Tosqlts.java,v 1.3 2004/03/19 11:57:15 benoitf 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.Timestamp 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  * Class to create tables for the 'osqlts' bean of 'etype'.
41  * @author Helene Joanin
42  */

43 public class Tosqlts extends Tmanager {
44
45     /**
46      * Entry point
47      */

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

56     private static void createTable(String JavaDoc name) throws RemoteException JavaDoc {
57
58         // get connection
59
Connection JavaDoc conn = null;
60         try {
61             conn = dataSource.getConnection();
62         } catch(Exception JavaDoc e) {
63             throw new RemoteException JavaDoc("Cannot get Connection");
64         }
65     
66         Statement JavaDoc stmt;
67         PreparedStatement JavaDoc pStmt;
68         try {
69             stmt = conn.createStatement();
70             stmt.execute("DROP TABLE "+name);
71             stmt.close();
72             logger.log(BasicLevel.INFO, "Table "+name+" dropped");
73         } catch(Exception JavaDoc e) {
74             logger.log(BasicLevel.DEBUG, "Exception in dropTable : \n"+e);
75         }
76         try {
77             int dbType = Env.getDatabaseType(conn);
78             String JavaDoc cTypeName;
79             switch (dbType) {
80             case Env.DB_ORACLE: cTypeName = "date"; break;
81             default: cTypeName = "timestamp"; // standard jdbc type name
82
}
83             stmt = conn.createStatement();
84             stmt.execute("create table " + name +
85                          "( c_pk varchar(30) not null primary key, c_f1 "+cTypeName+")");
86             stmt.close();
87             final long ONE_DAY = 24L*60L*60L*1000L;
88             pStmt = conn.prepareStatement("insert into "+name+" values('pk1', ?)");
89             pStmt.setTimestamp(1, new Timestamp JavaDoc(ONE_DAY));
90             pStmt.executeUpdate();
91             pStmt.close();
92             pStmt = conn.prepareStatement("insert into "+name+" values('pk2', ?)");
93             pStmt.setTimestamp(1, new Timestamp JavaDoc(2*ONE_DAY));
94             pStmt.executeUpdate();
95             pStmt.close();
96             pStmt = conn.prepareStatement("insert into "+name+" values('pk3', ?)");
97             pStmt.setTimestamp(1, new Timestamp JavaDoc(3*ONE_DAY));
98             pStmt.executeUpdate();
99             pStmt.close();
100             pStmt = conn.prepareStatement("insert into "+name+" values('pk4', ?)");
101             pStmt.setTimestamp(1, new Timestamp JavaDoc(4*ONE_DAY));
102             pStmt.executeUpdate();
103             pStmt.close();
104             pStmt = conn.prepareStatement("insert into "+name+" values('pk5', ?)");
105             pStmt.setTimestamp(1, new Timestamp JavaDoc(5*ONE_DAY));
106             pStmt.executeUpdate();
107             pStmt.close();
108             pStmt = conn.prepareStatement("insert into "+name+" values('pk5bis', ?)");
109             pStmt.setTimestamp(1, new Timestamp JavaDoc(5*ONE_DAY));
110             pStmt.executeUpdate();
111             pStmt.close();
112             pStmt = conn.prepareStatement("insert into "+name+" values('pktoremove', ?)");
113             pStmt.setTimestamp(1, new Timestamp JavaDoc(12*ONE_DAY));
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.setTimestamp(1, new Timestamp JavaDoc(12*ONE_DAY));
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