java - 如何在 servlet 中调用 DAO 方法来提取查询结果

标签 java jsp servlets jakarta-ee dao

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

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

我的 DAO 称为 BalanceDAO,如下所示:

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 并获取列表并使用 <c:forEach> 将它们呈现在 jsp 上, <c:out/>

另请参阅

关于java - 如何在 servlet 中调用 DAO 方法来提取查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5721344/

相关文章:

java - 在 Windows 上使用 Apache tomcat 运行 jsp 文件

java - 在 apache tomcat 中传输 body 之前分析 HTTP-header

java - 将一个对象的引用传递给另一个类中的另一个方法

java - 从java传递到HTML

java - 从 jdk 1.7 降级到 1.6 会停止我的 eclipse indigo 工作

spring - 无法访问Servlet中的Java Bean,getAttribute()返回null

java - 如何获取 blob 的 ContentType?

java - 支持部分对象更新的 Java 嵌入式数据库

Java Servlet 向 JSP 页面发送数据

Java - 为什么即使调用了有效资源,也会执行自定义 404 错误页面?