JavaWeb實(shí)現(xiàn)簡(jiǎn)單查詢商品功能
本文實(shí)例為大家分享了JavaWeb實(shí)現(xiàn)簡(jiǎn)單查詢商品功能的具體代碼,供大家參考,具體內(nèi)容如下
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>歡迎進(jìn)入管理頁面</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>"; // 登錄的時(shí)候進(jìn)行驗(yàn)證 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(); // 進(jìn)行數(shù)據(jù)庫訪問的類 // 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>登錄失?。?lt;/h2>2秒自動(dòng)跳轉(zhuǎn)到登錄頁面!"); response.setHeader("refresh", "2;url=login.html"); } } catch (Exception e) { e.printStackTrace(); } } else { pw.println("<h1>登錄失??!</h2>5秒自動(dòng)跳轉(zhuǎn)到登錄頁面!"); 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號(hào)碼</th>" + "<th>商品名稱</th>" + "<th>商品價(jià)格</th>" + "<th>商品庫存數(shù)量</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號(hào)碼</th>" + "<th>商品名稱</th>" + "<th>商品價(jià)格</th>" + "<th>商品庫存數(shù)量</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(); } } } } }
數(shù)據(jù)庫訪問類:
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"; // 注冊(cè)加載驅(qū)動(dòng) Class.forName(driverClass); // 獲取連接 Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); return conn; } // 登錄的時(shí)候 進(jìn)行驗(yàn)證 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); //查詢到記錄的時(shí)候,返回一個(gè)resultSet,也處理了該方法查找失敗的時(shí)候返回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"> 賬號(hào):<input type="text" name="admin_id"/> 密碼:<input type="password" name="password"/> <input type="submit" value="登錄" name="login"/> </form> </body> </html>
數(shù)據(jù)庫里面的表數(shù)據(jù)
運(yùn)行效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java+mysql實(shí)現(xiàn)商品搶購功能
- Java使用策略模式解決商場(chǎng)促銷商品問題示例
- java使用hadoop實(shí)現(xiàn)關(guān)聯(lián)商品統(tǒng)計(jì)
- JAVAEE model1模型實(shí)現(xiàn)商品瀏覽記錄(去除重復(fù)的瀏覽記錄)(一)
- java實(shí)現(xiàn)商品管理系統(tǒng)
- java實(shí)現(xiàn)超市商品庫存管理平臺(tái)
- java實(shí)現(xiàn)商品信息管理系統(tǒng)
- Java基于JDBC實(shí)現(xiàn)事務(wù),銀行轉(zhuǎn)賬及貨物進(jìn)出庫功能示例
- Java實(shí)現(xiàn)商品的查找、添加、出庫、入庫操作完整案例
相關(guān)文章
SpringBoot3?Web編程開發(fā)的工程搭建攔截器及測(cè)試工具示例
這篇文章主要介紹了SpringBoot3?Web編程開發(fā)的工程搭建攔截器及測(cè)試工具示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Java中HashTable和HashMap的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
HashTable和HashMap主要的區(qū)別有:線程安全性,同步(synchronization),以及速度。接下來通過本文給大家簡(jiǎn)單介紹下HashTable和HashMap的區(qū)別,需要的的朋友參考下吧2017-04-04Maven學(xué)習(xí)教程之搭建多模塊企業(yè)級(jí)項(xiàng)目
本篇文章主要介紹了Maven學(xué)習(xí)教程之搭建多模塊企業(yè)級(jí)項(xiàng)目 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Java中Object toString方法簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Object類在Java里面是一個(gè)比較特殊的類,JAVA為了組織這個(gè)類組織得比較方便,它提供了一個(gè)最根上的類,相當(dāng)于所有的類都是從這個(gè)類繼承,這個(gè)類就叫Object。接下來通過本文給大家介紹Object toString方法,需要的的朋友參考下吧2017-05-05java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解
這篇文章主要介紹了java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09java獲取文件擴(kuò)展名的方法小結(jié)【正則與字符串截取】
這篇文章主要介紹了java獲取文件擴(kuò)展名的方法,結(jié)合實(shí)例形式分析了使用正則與字符串截取兩種獲取擴(kuò)展名的操作技巧,需要的朋友可以參考下2017-01-01Java實(shí)現(xiàn)經(jīng)典游戲超級(jí)瑪麗的示例代碼
在你的童年記憶里,是否有一個(gè)蹦跳、頂蘑菇的小人?本文將用java語言實(shí)現(xiàn)經(jīng)典游戲《超級(jí)瑪麗》,文中采用了swing技術(shù)進(jìn)行了界面化處理,需要的可以參考一下2022-02-02