Java 如何在servlet中调用DAO方法以提取查询结果

Java 如何在servlet中调用DAO方法以提取查询结果,java,jsp,servlets,jakarta-ee,dao,Java,Jsp,Servlets,Jakarta Ee,Dao,我一直在努力找到正确的过程,从我的一个servlet中的DAO中提取查询结果,但没有成功 帮助解决我的问题将不胜感激 我的刀叫做平衡刀,如下所示: package HWpackage; import java.sql.*; import java.text.*; import java.util.*; public class BalanceDAO { static Connection currentConn = null; static ResultSet rsBalance = nu

我一直在努力找到正确的过程,从我的一个servlet中的DAO中提取查询结果,但没有成功

帮助解决我的问题将不胜感激

我的刀叫做平衡刀,如下所示:

package HWpackage;

import java.sql.*;
import java.text.*;
import java.util.*;

public class BalanceDAO
{

static Connection currentConn = null;
static ResultSet rsBalance = null;

public static BalanceBean total(BalanceBean bean)
{
//preparing some objects for connection
Statement stmt = null;
String id = bean.getID();
String balance = bean.getBalance();

String balanceQuery =
    "select balance as balance from users where id='"
        + id
        + "'";
    try
{
    //connect to DB
    currentConn = DBConnection.getConnection();
    stmt=currentConn.createStatement();
    rsBalance = stmt.executeQuery(balanceQuery);
    boolean more = rsBalance.next();

    // if user does not have a balance
    if (!more)
    {
        balance = "0.00";
    }

}

catch (Exception ex)
{
    System.out.println("Log In failed: An Exception has occurred! " + ex);
}

//some exception handling
finally
{
    if (rsBalance != null) {
        try {
            rsBalance.close();
        } catch (Exception e) {}
        rsBalance = null;
    }

    if (stmt != null) {
        try {
            stmt.close();
        } catch (Exception e) {}
        stmt = null;
    }

    if (currentConn != null) {
        try {
            currentConn.close();
        } catch (Exception e) {
        }

        currentConn = null;
    }
}

return bean;

}

}
我想将balanceQuery的结果拉入我的ViewAccounts servlet。 我试图使用一个名为“balance”的变量,以便out.println部分中包含“balance+”的行是有效的

package HWpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.*;
import javax.servlet.http.*;

public class ViewAccounts extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession();
        UserBean2 userBean2 = (UserBean2) session.getAttribute("userBean2");
        String id = userBean2.getUsername();



        try {

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ViewAccounts</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<form action=''>" +
                        "<table border ='5'>" +
                        "<tr>" +
                        "<th colspan='5'>" +
                        id +
                        ", you currently have 01 account available for stock transactions</th>" +
                        "</tr>" +
                        "<tr>" +
                        "<th>Checking</th>" +
                        "<th>$" +
                        // balance +
                        "</th>" +
                        "<th><input type='submit' value='Delete'/></th>" +
                        "<tr>" +
                        "<th colspan='3' style='text-align:right'><a href='AddBank'>[Add Account]</a><a href='categories.jsp'>[Categories]</a><a href='index.jsp'>[Log Out]</a></th>" +
                        "</tr>" +
                        "</table></form>");
            out.println("</body>");
            out.println("</html>");

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

这不是你做这件事的方式

  • 创建一个类来包装要显示的字段

  • 从servlet调用DAO/service并获取列表,然后使用
    在jsp上呈现它们

另请参见


我有一个Bean(模型)、一个Servlet(视图)和一个DAO(控制器),这不是使用MVC设计吗?不是。Servlet是控制器,JSP是视图。您没有JSP。DAO是另一个体系结构的一部分。不管出于什么原因,我的导师不希望我们在这个任务中使用JSP。对我的问题有什么想法吗?谢谢你的帮助。这是标准的方法,我不会鼓励你跟随你的导师。请详细说明你面临的实际问题。会发生什么?什么事不会发生?您会遇到哪些错误/异常?代码中有太多错误,我不知道从哪里开始根据代码回答。