這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用JavaWeb怎么實(shí)現(xiàn)一個(gè)商品查詢(xún)功能,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站是一家專(zhuān)注于成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),土默特左旗網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:土默特左旗等地區(qū)。土默特左旗做網(wǎng)站價(jià)格咨詢(xún):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 = ""; // 登錄的時(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ù)庫(kù)訪問(wèn)的類(lèi) // 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("歡迎進(jìn)入管理頁(yè)面
Id號(hào)碼 | " + "商品名稱(chēng) | " + "商品價(jià)格 | " + "商品庫(kù)存數(shù)量 | " + "商品描述 | "; String main = "
---|---|---|---|---|
" + mess[0] + " | " + "" + mess[1] + " | " + "" + mess[2] + " | " + "" + mess[3] + " | " + "" + mess[4] + " |
Id號(hào)碼 | " + "商品名稱(chēng) | " + "商品價(jià)格 | " + "商品庫(kù)存數(shù)量 | " + "商品描述 | "; 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] + " |
數(shù)據(jù)庫(kù)訪問(wèn)類(lèi):
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); //查詢(xún)到記錄的時(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
Insert title here 登錄
數(shù)據(jù)庫(kù)里面的表數(shù)據(jù)
運(yùn)行效果
上述就是小編為大家分享的使用JavaWeb怎么實(shí)現(xiàn)一個(gè)商品查詢(xún)功能了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。