I'm using a method to that get some informations from a database and return an Object with that data. When I use this method on a Servlet doGet the Object comes empty. When I do the same thing on a main it comes properly filled with the data. Does anyone knows what am I doing wrong, please?
Prints:
In doGet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// TODO Auto-generated method stub
Conta conta = ContaDAO.GetConta(2);
double entrada = conta.getEntrada();
request.setAttribute("entrada", entrada);
request.getRequestDispatcher("dashboard.jsp").forward(request, response);
}
In main:
public static void main(String[] Args) {
Conta conta = new Conta();
conta = ContaDAO.GetConta(2);
double entrada = conta.getEntrada();
System.out.println(conta.getEntrada());
System.out.println(entrada);
This is the Conta constructor model I'm using:
public class Conta{
//Atributos
protected int id;
protected String nomeDaConta;
protected double saldo;
protected double gasto;
protected double entrada;
/*protected ArrayList<String> categoriaDeGasto = new ArrayList<String>();
protected ArrayList<String> categoriaDeEntrada = new ArrayList<String>();*/
public Conta() {
super();
}
This is the ContaDao method that I'm using:
public class ContaDAO {
public ContaDAO() {
super();
}
public static Conta GetConta(int id){
//Connection conexao = null;
PreparedStatement ps = null;
ResultSet rs = null;
Conta conta = new Conta();
Connection conexao;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
// Com o driver do Oracle já inserido pegando a variavel conexao e passando as credenciais do Oracle
conexao = DriverManager.getConnection(
"jdbc:oracle:thin:@oracle.fiap.com.br:1521:ORCL", "RM552501", "270697");
String sqlGetConta = "SELECT DISTINCT CD_CONTA, NR_TOTAL_ENTRADA, NR_TOTAL_SAIDA FROM T_FINEASY_CONTA WHERE CD_CONTA = ? ORDER BY CD_CONTA";
ps = conexao.prepareStatement(sqlGetConta);
ps.setInt(1, id);
rs = ps.executeQuery();
while(rs.next()) {
conta.setId(rs.getInt("CD_CONTA"));
//System.out.println(conta.getId());
conta.setEntrada(rs.getInt("NR_TOTAL_ENTRADA"));
//System.out.println(conta.getEntrada());
conta.setGasto(rs.getInt("NR_TOTAL_SAIDA"));
//System.out.println(conta.getGasto());
}
DBConnection.closeconexao(conexao);
DBConnection.closeconexaoPS(ps);
DBConnection.closeconexaoRS(rs);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}
return conta;
}
It was a driver issue. Basically my oracle.jdbc driver was "installed" in the wrong folder structure. I had created a folder, transferred the driver file into this folder and added it to the build path.
PS: I'm working on a dynamic web project. The correct thing to do is to transfer the driver files to the folder: 'WebContent/WEB-INF/lib' according to the file structure that I'm using.
My professor said it's an issue with the Tomcat that only sees the driver in this way.
Thanks.