I have this singleton class of connection to an Oracle Database 11g express edition database:
public class ConexionDB {
private static ConexionDB _conexion;
private Connection connection;
/**
*
*/
private ConexionDB() {
}
public static ConexionDB getInstancia() {
if (_conexion == null) {
try {
Class.forName("oracle.jdbc.OracleDriver");
String url = ("jdbc:oracle:thin:@localhost:1521:XE");
Connection conn = (Connection) DriverManager.getConnection(url, "fukusuke","fukusuke");
ConexionDB conexion = new ConexionDB();
conexion.setConnection(conn);
_conexion = conexion;
} catch (Exception e) {
throw new ConexionException("Ocurrio un error"
+ " al conectar con la base de datos", e);
}
}
return _conexion;
}
/**
* @return the connection
*/
public Connection getConnection() {
return connection;
}
/**
* @param connection the connection to set
*/
public void setConnection(Connection connection) {
this.connection = connection;
}
}
And I try to call the data with this Dao:
public List<Ingrediente> lista() throws SQLException, ClassNotFoundException {
try{
ConexionDB conn = ConexionDB.getInstancia();
PreparedStatement consulta = conn.getConnection().prepareStatement("select * from ingrediente");
ResultSet rst = consulta.executeQuery();
List<Ingrediente> ingredientes = new ArrayList<Ingrediente>();
while(rst.next()){
ingredientes.add(parser(rst));
}
return ingredientes;
}catch(Exception ex){
System.out.println(ex.getMessage());
return null;
}
}
private Ingrediente parser(ResultSet rst) throws SQLException{
Ingrediente ing = new Ingrediente();
ing.setId_ingrediente(rst.getInt("id_ingrediente"));
ing.setNombre(rst.getString("nombre"));
ing.setFecha_elaboracion(rst.getTimestamp("fecha_elaboracion"));
ing.setFecha_vencimiento(rst.getTimestamp("fecha_vencimiento"));
ing.setFecha_registro(rst.getTimestamp("fecha_registro"));
ing.setEstado(rst.getString("estado"));
return ing;
}
When I test the connection in a class Test everything is fine and it brings me the data, but when I try to show them in a JSP it gives me an error of connection to the database, some help ?. Thank you very much already.