Я пытался найти правильную процедуру для извлечения результата запроса из 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.Я пытаюсь использовать переменную с именем «balance», чтобы строка, содержащая «balance +» в разделе out.println, была действительной.
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>
}