开发者

How to invoke a DAO method in a servlet to pull the result of a query

I've been trying to find the correct procedure to pull the result of a query from a DAO in one of my servlets but have been unsuccessful.

Help solving my problem will be greatly appreciated.

My DAO is called BalanceDAO and is as follows:

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;

}

}

I'd like to pull the result of the balanceQuery into my ViewAccounts servlet. I'm trying to use the a variable called "balance" so that the line that contains "balance +" in the out.println section is valid.

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>

}


This is not the way you do it,

  • Create a Class that wraps the fields you want to display

  • From servlet invoke DAO/service and get the List and render them on jsp using <c:forEach> ,<c:out/>

See Also

  • MVC Design pattern
  • Our Servlet Wiki page
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜