/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.jtests.tables;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.Statement;

import javax.naming.NamingException;

import org.objectweb.util.monolog.api.BasicLevel;

public class Tapplimet extends Tmanager {

    /**
     * Entry point
     */
    public static void init() throws NamingException, RemoteException {
        mgrInit();
        createTable();
    }

    private static void createTable() throws RemoteException {

        String name = "JT2_MARCHE";

        // get connection
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch(Exception e) {
            throw new RemoteException("Cannot get Connection");
        }
	
        Statement stmt;
        try {
            stmt = conn.createStatement();
            stmt.execute("DROP TABLE "+name);
            stmt.close();
            logger.log(BasicLevel.INFO, "Table "+name+" dropped");
        } catch(Exception e) {
            logger.log(BasicLevel.DEBUG, "Exception in dropTable : \n"+e);
        }
        try {
            stmt = conn.createStatement();
            stmt.execute("create table " + name + 
                         "(IDMAR integer not null primary key, NOM varchar(30))");
            stmt.execute("insert into "+name+" values(0, 'Philippe Coq')");
            stmt.execute("insert into "+name+" values(1, 'Philippe Durieux')");
            stmt.execute("insert into "+name+" values(2, 'Adriana Danes')");
            stmt.execute("insert into "+name+" values(3, 'Helene Joanin')");
            stmt.close();
            conn.close();
        } catch(Exception e) {
            logger.log(BasicLevel.ERROR, "Exception in createTable : \n"+e);
            throw new RemoteException("Exception in createTable : "+e);
        }
        logger.log(BasicLevel.INFO, "Table "+name+" created");
    }

}


