/*
 * 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.beans.applimet;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Dao {
    private Connection cnx = null;
    private ResultSet rs = null;
    private Statement st = null;

    public Dao() throws NamingException, SQLException {
        Context ctx = new InitialContext();
        DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc_1");
        cnx = ds.getConnection();
    }

    public String rechercherTousLesMarches() throws SQLException {
        String ret = "";
        st = cnx.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = st.executeQuery("SELECT * FROM JT2_MARCHE");
        if (rs.first()) {
            do {
                ret = rs.getInt("IDMAR") + ":" + rs.getString("NOM") + "\n";        
            } while(rs.next());
        }
        return ret;
    }

    public void removeConnexion() throws SQLException {
        if (st != null) {
            st.close();
        }
        if (cnx != null) {
            cnx.close();
        }
    }
}
