eclipse doGet Java Servlet中的空对象[重复]

qcuzuvrc  于 2023-11-18  发布在  Eclipse
关注(0)|答案(1)|浏览(180)

此问题在此处已有答案

How to install JDBC driver in servlet based project without facing java.lang.ClassNotFoundexception(13个回答)
12小时前关门了
我正在使用一个方法,从数据库中获取一些信息,并返回一个包含该数据的Object。当我在Servlet doGet上使用此方法时,Object为空。当我在main上做同样的事情时,它正确地填充了数据。有人知道我做错了什么吗?
打印:
in doGetin main
在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);
    
}

字符串
主要内容:

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);


这是我使用的Conta构造函数模型:

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();
}


这是我使用的ContaDao方法:

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;
}

sqxo8psd

sqxo8psd1#

这是一个驱动程序问题。基本上我的oracle.jdbc驱动程序是“安装”在wrog文件夹结构中。我已经创建了一个文件夹,将驱动程序文件转移到这个文件夹并添加到构建路径。Ps:我正在处理一个动态Web项目。正确的做法是将驱动程序文件转移到文件夹:根据我使用的文件结构,是“WebContent/WEB-INF/lib”。我的教授说这是Tomcat的问题,它只能以这种方式看到驱动程序。谢谢。

相关问题