超市销售系统

        这次数据库大作业做了一个超市销售系统,java后端,jsp前端,sqlserver数据库,从空项目自己搭了一个简单的web小网页可以增删改查,自己从web零基础一点点学起来。

        在做的过程中遇到了很多问题,经过慢慢的debug,查阅资料,好像有了一点眉目,我理解的大流程应该是前端获取到对应指令,返回到后端,后端对数据进行处理,对数据库里的数据进行增删改查,我用的是sql语句,但是据老师说,现在java里一些语句处理起来会更快,更方便,后端处理完数据后,把结果或者说相应数据返回给前端,前端页面进行更改。在这个基础上我试着与单片机进行通讯,用的是RXTX库,逻辑就更加清晰了,也成功实现了从前端到后端到数据库和单片机通讯,再到后端处理,返回到前端。

        这是一个简单的登录界面,其中背景和表情包都是gif格式,可以自行更换。

<body style = "background: url(../static/images/log3.gif)">
<span class="first-text">这是一个超市</span>
<span class="second-text">放心选购,包您满意</span>

<div class="title">超市销售系统</div>
<div class="login-box">
  <form action="login.jsp" method="post">
    <input type="text" placeholder="用户名" name = "id" required>
    <input type="password" placeholder="密码" name = "password" required>
  <button type = "submit">登录</button>
</form>
</div>
<img class="animated_left-image" src="../static/images/海绵宝宝.gif" alt="动态图像">
<img class="animated_right-image" src="../static/images/秃头.gif" alt="动态图像">
</body>

这里的用户名与密码我存在了数据库的一个表中,通过与数据库的连接可以登录进入系统,如果密码错误会进入到另一个界面,jsp可以直接写java语句,所以一开始我把他们写在了一起。

<!DOCTYPE html>
<html>
<head>
  <title>数据处理页面</title>
</head>
<body>
<%
  String userName = request.getParameter("id");
  String password = request.getParameter("password");
  System.out.println(userName + "username");
  System.out.println(password + "Apassword");
  Connection con = null;
  Statement st = null;
  ResultSet rs = null;
  if(userName == null || userName.trim().isEmpty()) {
    // 检查响应是否已经被提交,如果没有被提交,则重定向到index.jsp
    if(!response.isCommitted()){
      response.sendRedirect("index.jsp");
      System.out.println("back");
    } else {
      // 在这里可以选择记录错误信息或者抛出异常等其他处理方式
      // 这里选择直接返回空页面,不进行重定向
      out.println("Invalid request parameters");
    }
  }
  try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Supermarket_system;TrustServerCertificate=true";
    String username="sa";
    String Password="123456";
    con = DriverManager.getConnection(url, username, Password);
    System.out.println(con + "test");
    st = con.createStatement();
    System.out.println("st:" + st);
    String query = "select * from Administrators where A_id=?";

    PreparedStatement pst = con.prepareStatement(query);
    System.out.println("password1:" + password);

    pst.setString(1, password);
    System.out.println("password2:" + password);


    System.out.println("pst:" + pst);

    rs = pst.executeQuery();
    System.out.println("rs:" + rs);

    System.out.println("rs.next()" + rs.next());
    if (true/*rs.next()*/) {
      String query2 = "select * from Administrators where Apassword=?";
      PreparedStatement pst2 = con.prepareStatement(query2);
      System.out.println("pst2:" + pst);

      pst2.setString(1, password);
      rs = pst2.executeQuery();
      System.out.println("rs.next()" + rs.next());
      if (rs.next()) {
        System.out.println(rs.next());
        if(!response.isCommitted()){
          response.sendRedirect("customer/customManager.jsp");
          System.out.println("yes");
        } else {
          out.println("Error: Redirect failed because response is already committed");
        }
      } else {
        out.println("Invalid password");
      }
    } else {
      out.println("Invalid user name");
    }
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    try {
      if (rs != null){rs.close();}
      if (st != null) {st.close();}
      if (con != null) {con.close();}
    } catch (SQLException e) {
      e.printStackTrace(); // 可以考虑使用日志库记录异常信息
    }
  }
%>
</body>
</html>

跑是可以跑的,但是后面问了一下老师,老师说后端的代码不要跟前端写在一起,会造成数据泄露,我一听感觉很有道理,这个时候思路不是很清楚,网上学习了一下就直接写了,本来是打算做完后面的页面再来改,后面时间不太够了,忘记了这件事情。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值