当前位置: 移动技术网 > IT编程>开发语言>Java > JavaWeb实现简单查询商品功能

JavaWeb实现简单查询商品功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

大叔自创抽筋舞,9713,宫筱轩个人资料

本文实例为大家分享了javaweb实现简单查询商品功能的具体代码,供大家参考,具体内容如下

customerservlet.java

package com.subing.web;

import java.io.ioexception;
import java.io.printwriter;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;


@webservlet("/customerservlet")
public class customerservlet extends httpservlet {
  private static final long serialversionuid = 1l;

  private sqldemo sql = null;
  private final static string s1 = "<h1>欢迎进入管理页面</h1><form action='customerservlet' method='post'> "
      + "精确查询:<input type='text' name='jqmess'/><br>"
      + "模糊查询:<input type='text' name='mhmess'/><br>"
      + "<input type='submit' value='提交' name='sub'/>"
      + "</form>";


  // 登录的时候进行验证
  private boolean isloginprov(string userinfo, string password) {
    if (userinfo != null && userinfo.length() > 0 && password != null
        && password.length() > 0) {
      return true;
    }
    return false;

  }

  public customerservlet() throws exception {
    super();
    sql = new sqldemo(); // 进行数据库访问的类
    // todo auto-generated constructor stub
  }

  /**
   * @see httpservlet#doget(httpservletrequest request, httpservletresponse
   *   response)
   */
  protected void doget(httpservletrequest request,
      httpservletresponse response) throws servletexception, ioexception {
    // todo auto-generated method stub
    this.dopost(request, response);
  }

  /**
   * @see httpservlet#dopost(httpservletrequest request, httpservletresponse
   *   response)
   */
  protected void dopost(httpservletrequest request,
      httpservletresponse response) throws servletexception, ioexception {
    response.setcontenttype("text/html;charset=gb2312");
    request.setcharacterencoding("gb2312");
    printwriter pw = response.getwriter();

    string sub = request.getparameter("sub");
    string login = request.getparameter("login");

    if (login != null && login.length() > 0) {
      string admin_id = request.getparameter("admin_id");
      string password = request.getparameter("password");
      if (isloginprov(admin_id, password)) {
        try {
          if (sql.loginverify(admin_id, password)) {
            pw.println(s1);
          } else {
            pw.println("<h1>登录失败!</h2>2秒自动跳转到登录页面!");
            response.setheader("refresh", "2;url=login.html");
          }
        } catch (exception e) {
          e.printstacktrace();
        }
      } else {
        pw.println("<h1>登录失败!</h2>5秒自动跳转到登录页面!");
        response.setheader("refresh", "5;url=login.html");
      }
    } else if (sub != null && sub.length() > 0) {
      pw.println(s1);
      string jqmess = request.getparameter("jqmess");
      string mhmess = request.getparameter("mhmess");
      if (jqmess != null && jqmess.length() > 0) {
        try {
          string s = sql.getjqmess(jqmess);
          string mess[] = s.split(",");
          string html = "<table border='5'>" + "<tr>" + "<th>id号码</th>"
              + "<th>商品名称</th>" + "<th>商品价格</th>"
              + "<th>商品库存数量</th>" + "<th>商品描述</th>";
          string main = "<tr>" + "<td>" + mess[0] + "</td>" + "<td>"
              + mess[1] + "</td>" + "<td>" + mess[2] + "</td>"
              + "<td>" + mess[3] + "</td>" + "<td>" + mess[4]
              + "</td></tr></table>";
          string head = html + main;
          pw.println(head);
        } catch (exception e) {
          e.printstacktrace();
        }
      } else if (mhmess != null && mhmess.length() > 0) {
        try {
          string head = "";
          string html = "<table border='5'>" + "<tr>" + "<th>id号码</th>"
              + "<th>商品名称</th>" + "<th>商品价格</th>"
              + "<th>商品库存数量</th>" + "<th>商品描述</th>";
          head += html;
          string s = sql.getmhmess(mhmess);
          string m[] = s.split(",,");
          for (int i = 0; i < m.length; i++) {
            string mess[] = m[i].split(",");
            string main = "<tr>" + "<td>" + mess[0] + "</td>"
                + "<td>" + mess[1] + "</td>" + "<td>" + mess[2]
                + "</td>" + "<td>" + mess[3] + "</td>" + "<td>"
                + mess[4] + "</td></tr>";
            head += main;
          }
          head += "</table>";
          pw.println(head);
        } catch (exception e) {
          e.printstacktrace();
        }
      }
    }
  }
}

数据库访问类:
sqldemo.java

package com.subing.web;

import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;

public class sqldemo {

  private connection conn = null;
  private preparedstatement preparedstatement = null;

  public sqldemo() throws exception {
    conn = getconnection();
  }

  private connection getconnection() throws exception {
    string driverclass = "com.mysql.jdbc.driver";
    string url = "jdbc:mysql:///shop?useunicode=true&characterencoding=gb2312";
    string user = "root";
    string password = "12345";

    // 注册加载驱动
    class.forname(driverclass);

    // 获取连接
    connection conn = drivermanager.getconnection(url, user, password);
    system.out.println(conn);
    return conn;
  }

  // 登录的时候 进行验证
  public boolean loginverify(string userinfo, string password)
      throws exception {
    string sql = "select * from admin where admin_id = ?";
    preparedstatement = conn.preparestatement(sql);
    preparedstatement.setstring(1, userinfo);
    resultset rs = preparedstatement.executequery();
    if (rs.next()) {
      if (rs.getstring("password").equals(password)) {
        system.out.println("成功!");
        return true;
      }
    }
    system.out.println("失败!");
    return false;
  }

  public string getjqmess(string admin_id) throws exception {
    string s = "";
    string sql = "select * from product1 where product_id = ? or product_name like ?"
        + "or product_price like ?"
        + "or product_num like ?"
        + "or product_describe like ?";
    preparedstatement = conn.preparestatement(sql);
    preparedstatement.setstring(1, admin_id);
    preparedstatement.setstring(2, admin_id);
    preparedstatement.setstring(3, admin_id);
    preparedstatement.setstring(4, admin_id);
    preparedstatement.setstring(5, admin_id);
    //查询到记录的时候,返回一个resultset,也处理了该方法查找失败的时候返回null的情况
    resultset rs = preparedstatement.executequery();
    while (rs.next()) {
      s = rs.getint(1) + "," + rs.getstring(2) + "," + rs.getint(3) + ","
          + rs.getint(4) + "," + rs.getstring(5);
    }
    return s;

  }

  public string getmhmess(string admin_id) throws exception {
    string mess = "";
    string sql = "select * from product1 where product_id like ? or product_name like ? or product_price like ? or product_num like ?"
        + "or product_describe like ?";
    preparedstatement = conn.preparestatement(sql);
    preparedstatement.setstring(1, "%" + admin_id + "%");
    preparedstatement.setstring(2, "%" + admin_id + "%");
    preparedstatement.setstring(3, "%" + admin_id + "%");
    preparedstatement.setstring(4, "%" + admin_id + "%");
    preparedstatement.setstring(5, "%" + admin_id + "%");
    resultset rs = preparedstatement.executequery();
    while (rs.next()) {
      string s = rs.getint(1) + "," + rs.getstring(2) + ","
          + rs.getint(3) + "," + rs.getint(4) + "," + rs.getstring(5);
      mess += s + ",,";
    }
    return mess;
  }

  public static void main(string[] args) throws exception {
    sqldemo sqldemo = new sqldemo();
    string s = sqldemo.getmhmess("xi");
    string m[] = s.split(",,");
    for (int i = 0; i < m.length; i++) {
      system.out.println(m[i]);
    }
  }

}

html文件:
login.html

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
</head>
<body>
  <h1>登录</h1>
  <form action="customerservlet" method="post">
    账号:<input type="text" name="admin_id"/>
    密码:<input type="password" name="password"/>
    <input type="submit" value="登录" name="login"/>
  </form>        

</body>
</html>

数据库里面的表数据

这里写图片描述

运行效果

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网