KickJava   Java API By Example, From Geeks To Geeks.

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


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: Tosqldate.java,v 1.7 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.Date JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.Statement JavaDoc;
33
34 import javax.naming.NamingException JavaDoc;
35
36 import org.objectweb.util.monolog.api.BasicLevel;
37
38 /**
39  * Class to create tables for the 'osqldate' bean of 'etype'.
40  * @author Helene Joanin
41  */

42 public class Tosqldate extends Tmanager {
43
44     /**
45      * Entry point
46      */

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

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