0

I'm building a program in java that runs through a Jboss 5.0.1 application server. The program has to consult and update tables in an Oracle 10g database but I'm having a lot of trouble with it.

Basically, i have a class called DAO that connects whith the database and makes a simple query through the method "prueba()". Here's the implementation of the class:

package dao;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;

import valueObject.ItemFisico;



public class DAO {

//----------------------------------------------------
//Constantes
//----------------------------------------------------
/**
 * ruta donde se encuentra el archivo de conexion.
 */
public final static String ARCHIVO_PROPIEDADES = "./data/conexion.properties";

//----------------------------------------------------
//Atributos
//----------------------------------------------------
/**
 * conexion con la base de datos
 */
public Connection conexion;

/**
 * nombre del usuario para conectarse a la base de datos.
 */
private String usuario;

/**
 * clave de conexion a la base de datos.
 */
private String clave;

/**
 * URL al cual se debe conectar para acceder a la base de datos.
 */
private String cadenaConexion;

/**
 * constructor de la clase. No inicializa ningun atributo.
 */
public DAO() 
{       

}

// -------------------------------------------------
// Metodos
// -------------------------------------------------

/**
 * obtiene ls datos necesarios para establecer una conexion
 * Los datos se obtienen a partir de un archivo properties.
 */
public void inicializar()
{
    try
    {
        File arch= new File(ARCHIVO_PROPIEDADES);
        Properties prop = new Properties();
        FileInputStream in = new FileInputStream( arch );

        prop.load( in );
        in.close( );

        cadenaConexion = prop.getProperty("url");   
        usuario = prop.getProperty("usuario");  
        clave = prop.getProperty("clave");  
        final String driver = prop.getProperty("driver");
        Class.forName(driver);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }   
}

/**
 * Metodo que se encarga de crear la conexion con el Driver Manager
 * a partir de los parametros recibidos.
 * @param url direccion url de la base de datos a la cual se desea conectar
 * @param usuario nombre del usuario que se va a conectar a la base de datos
 * @param clave clave de acceso a la base de datos
 * @throws SQLException si ocurre un error generando la conexion con la base de datos.
 */
private void establecerConexion(String url, String usuario, String clave) throws SQLException
{
    try
    {
        conexion = DriverManager.getConnection(url,usuario,clave);
    }
    catch( SQLException exception )
    {
        throw new SQLException( "ERROR: ConsultaDAO obteniendo una conexion." );
    }
}

/**
 *Cierra la conexion activa a la base de datos. Ademas, con=null.
 * @param con objeto de conexion a la base de datos
 * @throws SistemaCinesException Si se presentan errores de conexion
 */
public void closeConnection(Connection connection) throws Exception {        
    try {
        connection.close();
        connection = null;
    } catch (SQLException exception) {
        throw new Exception("ERROR: ConsultaDAO: closeConnection() = cerrando una conexion.");
    }
} 

// ---------------------------------------------------
// Metodos asociados a los casos de uso: Consulta
// ---------------------------------------------------  

public void prueba() throws Exception{
    String prueba = "SELECT * FROM PARRANDEROS.BARES b where b.presupuesto='Bajo' ";


    PreparedStatement st=null;

    try{
        inicializar();
        establecerConexion(cadenaConexion, usuario, clave);
        st = conexion.prepareStatement(prueba);

        ResultSet r= st.executeQuery(prueba);
        while(r.next()){
            System.out.println(r.getInt("ID")+":"+r.getString("NOMBRE")+":"+r.getString("CIUDAD")+":"+r.getString("PRESUPUESTO")+":"+r.getString("CANT_SEDES"));

        }

    }
    catch(Exception e)
    {
        e.printStackTrace();

    }

    finally{
        if (st != null) 
        {
            try {
                st.close();
            } catch (SQLException exception) {

                throw new Exception("ERROR: ConsultaDAO: loadRow() =  cerrando una conexi�n.");
            }
        }
        closeConnection(conexion);
    }

}


}

Here's the thing, everything in this class works fine, i have run it as a Java application through a simple main() method and it performs the query just fine. The driver .jar is well placed and referenced and all properties used for the connection are fine.

Now, when I run the whole application (including other classes like servlets and so) mounted on the running server, and then try to run again the method "prueba()", it crashes. It gets a FileNotFoundException caused by the conexion.properties file, which exists!!!.

I am not really an expert on web applications and don't really know if i have to make any changes on the server configuration or in another place. Can someone help me?

1 Answer 1

1

The problem is that you are using absolute path ./data/conexion.properties which is fine when you run the program locally. But deploying to an application server, would make the path invalid because the file will be in some other location (e.g. /apps/jboss/.../myapp/data/conexion.properties). In the inicializar method replace the following

File arch= new File(ARCHIVO_PROPIEDADES);

with

URL url = getClass().getClassLoader().getResource("conexion.properties");
File arch= new File(url.getFile());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.