老规矩,先看前端页面,把前端页面上没有的表达式都删掉:
创建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();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected 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;
}
测试一下就发现余额查询成功啦~
文章来源:https://www.toymoban.com/news/detail-559064.html
文章来源地址https://www.toymoban.com/news/detail-559064.html
到了这里,关于基于Javaweb实现ATM机系统开发实战(八)实时查询余额功能实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!