老规矩,先看前端页面,把前端页面上没有的表达式都删掉:
创建servlet接受和处理请求:
package com.atm.servlet;import com.atm.pojo.User;
import com.atm.service.UserService;
import com.atm.service.impl.UserServiceImpl;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;//查询用户余额
@WebServlet("/UserSerch")
public class UserSerchServlet extends HttpServlet {private UserService userService=new UserServiceImpl();@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String icno=null;if(request.getSession().getAttribute("ua")!=null){User user=(User)(request.getSession().getAttribute("ua"));icno=user.getIcno();}try {double balance=userService.getBalance(icno);
// request.setAttribute("balance",balance);
// request.getRequestDispatcher("/changepage/SerchBalance.jsp").forward(request,response);request.getSession().setAttribute("balance",balance);response.sendRedirect("/changepage/SerchBalance.jsp");}catch (Exception e){e.printStackTrace();}}
}
接口:
//查询用户余额public double getBalance(String icno)throws Exception;
实现类:
//查询用户余额public double getBalance(String icno)throws Exception{return userDao.getBalance(icno);}
数据层:
//查询用户余额public double getBalance(String icno) throws Exception{Connection connection= DBUtils.getConnection();PreparedStatement preparedStatement = connection.prepareStatement("select balance from user where icno=?");preparedStatement.setString(1,icno);ResultSet resultSet= preparedStatement.executeQuery();double balance=0;if(resultSet.next()){balance=resultSet.getDouble(1);}DBUtils.release(connection,preparedStatement,null);return balance;}
测试一下就发现余额查询成功啦~