001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: Tosqlts.java 4406 2004-03-19 11:57:20Z benoitf $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.tables;
027:
028: import java.rmi.RemoteException;
029: import java.sql.Connection;
030: import java.sql.PreparedStatement;
031: import java.sql.Statement;
032: import java.sql.Timestamp;
033:
034: import javax.naming.NamingException;
035:
036: import org.objectweb.jonas.jtests.util.Env;
037: import org.objectweb.util.monolog.api.BasicLevel;
038:
039: /**
040: * Class to create tables for the 'osqlts' bean of 'etype'.
041: * @author Helene Joanin
042: */
043: public class Tosqlts extends Tmanager {
044:
045: /**
046: * Entry point
047: */
048: public static void init() throws NamingException, RemoteException {
049: mgrInit();
050: createTable("JT_EtypeOsqltsEC");
051: }
052:
053: /**
054: * create a table for the osqlts bean of etype
055: */
056: private static void createTable(String name) throws RemoteException {
057:
058: // get connection
059: Connection conn = null;
060: try {
061: conn = dataSource.getConnection();
062: } catch (Exception e) {
063: throw new RemoteException("Cannot get Connection");
064: }
065:
066: Statement stmt;
067: PreparedStatement pStmt;
068: try {
069: stmt = conn.createStatement();
070: stmt.execute("DROP TABLE " + name);
071: stmt.close();
072: logger.log(BasicLevel.INFO, "Table " + name + " dropped");
073: } catch (Exception e) {
074: logger.log(BasicLevel.DEBUG, "Exception in dropTable : \n"
075: + e);
076: }
077: try {
078: int dbType = Env.getDatabaseType(conn);
079: String cTypeName;
080: switch (dbType) {
081: case Env.DB_ORACLE:
082: cTypeName = "date";
083: break;
084: default:
085: cTypeName = "timestamp"; // standard jdbc type name
086: }
087: stmt = conn.createStatement();
088: stmt.execute("create table " + name
089: + "( c_pk varchar(30) not null primary key, c_f1 "
090: + cTypeName + ")");
091: stmt.close();
092: final long ONE_DAY = 24L * 60L * 60L * 1000L;
093: pStmt = conn.prepareStatement("insert into " + name
094: + " values('pk1', ?)");
095: pStmt.setTimestamp(1, new Timestamp(ONE_DAY));
096: pStmt.executeUpdate();
097: pStmt.close();
098: pStmt = conn.prepareStatement("insert into " + name
099: + " values('pk2', ?)");
100: pStmt.setTimestamp(1, new Timestamp(2 * ONE_DAY));
101: pStmt.executeUpdate();
102: pStmt.close();
103: pStmt = conn.prepareStatement("insert into " + name
104: + " values('pk3', ?)");
105: pStmt.setTimestamp(1, new Timestamp(3 * ONE_DAY));
106: pStmt.executeUpdate();
107: pStmt.close();
108: pStmt = conn.prepareStatement("insert into " + name
109: + " values('pk4', ?)");
110: pStmt.setTimestamp(1, new Timestamp(4 * ONE_DAY));
111: pStmt.executeUpdate();
112: pStmt.close();
113: pStmt = conn.prepareStatement("insert into " + name
114: + " values('pk5', ?)");
115: pStmt.setTimestamp(1, new Timestamp(5 * ONE_DAY));
116: pStmt.executeUpdate();
117: pStmt.close();
118: pStmt = conn.prepareStatement("insert into " + name
119: + " values('pk5bis', ?)");
120: pStmt.setTimestamp(1, new Timestamp(5 * ONE_DAY));
121: pStmt.executeUpdate();
122: pStmt.close();
123: pStmt = conn.prepareStatement("insert into " + name
124: + " values('pktoremove', ?)");
125: pStmt.setTimestamp(1, new Timestamp(12 * ONE_DAY));
126: pStmt.executeUpdate();
127: pStmt.close();
128: pStmt = conn.prepareStatement("insert into " + name
129: + " values('pknull', NULL)");
130: pStmt.executeUpdate();
131: pStmt.close();
132: pStmt = conn.prepareStatement("insert into " + name
133: + " values('pkchangenull', ?)");
134: pStmt.setTimestamp(1, new Timestamp(12 * ONE_DAY));
135: pStmt.executeUpdate();
136: pStmt.close();
137: conn.close(); // release connection
138: } catch (Exception e) {
139: logger.log(BasicLevel.ERROR, "Exception in createTable : "
140: + e);
141: throw new RemoteException("Exception in createTable : " + e);
142: }
143: logger.log(BasicLevel.INFO, "Table " + name + " created");
144: }
145:
146: }
|