使用JavaWeb怎么实现一个商品查询功能
这期内容当中小编将会给大家带来有关使用JavaWeb怎么实现一个商品查询功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联建站是一家专注于成都做网站、网站制作、成都外贸网站建设与策划设计,土默特左旗网站建设哪家好?创新互联建站做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:土默特左旗等地区。土默特左旗做网站价格咨询:13518219792
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 = ""; // 登录的时候进行验证 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("欢迎进入管理页面
登录失败!2秒自动跳转到登录页面!");
response.setHeader("refresh", "2;url=login.html");
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
pw.println("登录失败!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 = "
Id号码 | " + "商品名称 | " + "商品价格 | " + "商品库存数量 | " + "商品描述 | "; String main = "
---|---|---|---|---|
" + mess[0] + " | " + "" + mess[1] + " | " + "" + mess[2] + " | " + "" + mess[3] + " | " + "" + mess[4] + " |
Id号码 | " + "商品名称 | " + "商品价格 | " + "商品库存数量 | " + "商品描述 | "; 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 = "
---|---|---|---|---|
" + mess[0] + " | " + "" + mess[1] + " | " + "" + mess[2] + " | " + "" + mess[3] + " | " + "" + mess[4] + " |
数据库访问类:
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
Insert title here 登录
数据库里面的表数据
运行效果
上述就是小编为大家分享的使用JavaWeb怎么实现一个商品查询功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
网站标题:使用JavaWeb怎么实现一个商品查询功能
标题URL:http://hbruida.cn/article/gospoh.html