我一直在尝试找到正确的过程来从我的一个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>
}
答案 0 :(得分:2)
这
。创建一个包装您要显示的字段的类
从servlet调用DAO / service并获取List并使用<c:forEach>
,<c:out/>
另见