大连可以做网站的公司北京公司注册最新政策
news/
2025/10/6 13:58:22/
文章来源:
大连可以做网站的公司,北京公司注册最新政策,网站用户反馈,免费素材网站哪个最好用Jsp Servlet和Jquery以及SQLServer实现商品管理系统。
采用MVC三层架构进行此程序的开发。
功能分析#xff1a; 1.登录功能 2.查询和联合查询商品 3.增加商品 4.删除商品
效果演示
登录页面 查询页面 添加商品 点击删除
实现商品管理系统需要数据库和程序两部分。…用Jsp Servlet和Jquery以及SQLServer实现商品管理系统。
采用MVC三层架构进行此程序的开发。
功能分析 1.登录功能 2.查询和联合查询商品 3.增加商品 4.删除商品
效果演示
登录页面 查询页面 添加商品 点击删除
实现商品管理系统需要数据库和程序两部分。
一 数据库
参考下面图片进行设计和实现SQLserver数据库
二程序设计
我们先看一下程序目录结构 特别注意 需要引入sqljdbc.jar和jquery.js两个架包
完整代码
servlet包 servlet包里DeleteServlet.java
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hnpi.util.DBUtil;public class DeleteServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType(text/html);PrintWriter out response.getWriter();String id request.getParameter(id);String sql delete from Product where ProductID ?;Connection conn DBUtil.getconn();try {System.out.println(sql);PreparedStatement ps conn.prepareStatement(sql);ps.setInt(1,Integer.parseInt(id));ps.executeUpdate();response.sendRedirect(list);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}out.flush();out.close();}}
servlet包里InsertServlet.java
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hnpi.util.DBUtil;public class InsertServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType(text/html);PrintWriter out response.getWriter();String ProductName new String(request.getParameter(ProductName).getBytes(iso-8859-1),utf-8);String IsUp new String(request.getParameter(IsUp).getBytes(iso-8859-1),utf-8);String UnitPrice new String(request.getParameter(UnitPrice).getBytes(iso-8859-1),utf-8);String Remark new String(request.getParameter(Remark).getBytes(iso-8859-1),utf-8);String CategoryID new String(request.getParameter(CategoryID).getBytes(iso-8859-1),utf-8);String sql insert into Product values(?,?,?,?,?);Connection conn DBUtil.getconn();try {System.out.println(sql);PreparedStatement ps conn.prepareStatement(sql);ps.setString(1,ProductName);ps.setString(2,IsUp);ps.setInt(3, Integer.parseInt(UnitPrice));ps.setString(4,Remark);ps.setInt(5, Integer.parseInt(CategoryID));if(ps.executeUpdate()0){response.sendRedirect(list);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}out.flush();out.close();}}
servlet包里ListServlet.java
package com.hnpi.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.hnpi.util.DBUtil;public class ListServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding(utf-8);response.setCharacterEncoding(utf-8);PrintWriter out response.getWriter();//接受从查询页面传过来的值String ProductName request.getParameter(ProductName);String Remark request.getParameter(Remark);HttpSession session request.getSession();//联合查询// select b表的所有a表的名字 from a表 as a, b表 as b where b.b表的id a.a表的idString sql select b.* ,a.CategoryName from Category as a , Product as b where b.CategoryID a.CategoryID;//判断是否为空if(!.equals(ProductName) ProductName!null){ProductName new String(ProductName.getBytes(iso-8859-1),utf-8);sql sql and b.ProductName \ProductName\;}if(!.equals(Remark) Remark!null){Remark new String(Remark.getBytes(iso-8859-1),utf-8);sql sql and b.Remark \Remark\;}//列表Connection conn DBUtil.getconn();try {System.out.println(sql);PreparedStatement ps conn.prepareStatement(sql);ResultSet rs ps.executeQuery();session.setAttribute(set, rs);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//类别try {String sq select * from Category;PreparedStatement ps conn.prepareStatement(sq);ResultSet rs ps.executeQuery();session.setAttribute(se, rs);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}response.sendRedirect(list.jsp);out.flush();out.close();}}
servlet包里LoginServlet.java
package com.hnpi.servlet;import java.awt.image.DataBufferUShort;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.hnpi.util.DBUtil;public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding(utf-8);response.setCharacterEncoding(utf-8);String name request.getParameter(name);String pwd request.getParameter(pwd);Connection conn DBUtil.getconn();PreparedStatement ps null;ResultSet rs null;String sql select * from Userinfo where UserName ? and Password ?;try {ps conn.prepareStatement(sql);ps.setString(1, name);ps.setString(2, pwd);rs ps.executeQuery();if(rs.next()){response.sendRedirect(list);}else{response.sendRedirect(login.jsp);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
util包 util包里DBUtil.java
package com.hnpi.util;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class DBUtil {public static Connection getconn(){try {Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}Connection conn null;try {conn DriverManager.getConnection(jdbc:sqlserver://localhost:1433;databaseNameTest,sa,1);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}
}
WEB-INF包 web.xml
?xml version1.0 encodingUTF-8?
web-app version2.5 xmlnshttp://java.sun.com/xml/ns/javaee xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsddisplay-name/display-nameservletdescriptionThis is the description of my J2EE component/descriptiondisplay-nameThis is the display name of my J2EE component/display-nameservlet-nameLoginServlet/servlet-nameservlet-classcom.hnpi.servlet.LoginServlet/servlet-class/servletservletdescriptionThis is the description of my J2EE component/descriptiondisplay-nameThis is the display name of my J2EE component/display-nameservlet-nameListServlet/servlet-nameservlet-classcom.hnpi.servlet.ListServlet/servlet-class/servletservletdescriptionThis is the description of my J2EE component/descriptiondisplay-nameThis is the display name of my J2EE component/display-nameservlet-nameDeleteServlet/servlet-nameservlet-classcom.hnpi.servlet.DeleteServlet/servlet-class/servletservletdescriptionThis is the description of my J2EE component/descriptiondisplay-nameThis is the display name of my J2EE component/display-nameservlet-nameInsertServlet/servlet-nameservlet-classcom.hnpi.servlet.InsertServlet/servlet-class/servletservlet-mappingservlet-nameLoginServlet/servlet-nameurl-pattern/login/url-pattern/servlet-mappingservlet-mappingservlet-nameListServlet/servlet-nameurl-pattern/list/url-pattern/servlet-mappingservlet-mappingservlet-nameDeleteServlet/servlet-nameurl-pattern/del/url-pattern/servlet-mappingservlet-mappingservlet-nameInsertServlet/servlet-nameurl-pattern/insert/url-pattern/servlet-mapping welcome-file-listwelcome-filelogin.jsp/welcome-file/welcome-file-list
/web-app
Jsp页面 add.jsp
%page importjava.sql.ResultSet%
% page languagejava importjava.util.* pageEncodingutf-8%
%
String path request.getContextPath();
String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;
%!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
htmlheadbase href%basePath%titleMy JSP add.jsp starting page/titlemeta http-equivpragma contentno-cachemeta http-equivcache-control contentno-cachemeta http-equivexpires content0 meta http-equivkeywords contentkeyword1,keyword2,keyword3meta http-equivdescription contentThis is my page!--link relstylesheet typetext/css hrefstyles.css--/headbodyform actioninsert商品名称input idProductName name ProductName/br/是否上架input idIsUp typeradio name IsUp value是/是input typeradio name IsUp value否/否br/单价input typenumber idUnitPrice name UnitPrice/br/备注textarea idRemark rows5 cols20 nameRemark/textareabr/所属类别:select idCategoryID name CategoryID% ResultSet rs (ResultSet)session.getAttribute(se);while(rs.next()){%option value%rs.getString(1) %%rs.getString(2) %/option%}%/selectbr/input typesubmit value提交/form/bodyscript typetext/javascript srcjquery-3.3.1.js/scriptscript typetext/javascript
$(function(){$(form).submit(function(){var ProductName $(#ProductName).val();var IsUp $(#IsUp).val();var UnitPrice $(#UnitPrice).val();var Remark $(#Remark).val();if(ProductName||IsUp||UnitPrice||Remark){alert(请补充完整信息)return false;}else{return true;}})
})/body
/html
list.jsp
%page importjava.sql.ResultSet%
% page languagejava importjava.util.* pageEncodingutf-8%
%
String path request.getContextPath();
String basePath request.getScheme()://request.getServerName():request.getServerPort()path/;
%!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
htmlheadbase href%basePath%titleMy JSP list.jsp starting page/titlemeta http-equivpragma contentno-cachemeta http-equivcache-control contentno-cachemeta http-equivexpires content0 meta http-equivkeywords contentkeyword1,keyword2,keyword3meta http-equivdescription contentThis is my page!--link relstylesheet typetext/css hrefstyles.css--/headbodycenterform actionlist商品名称input name ProductName商品备注input name Remarkinput typesubmit value查询buttona hrefadd.jsp添加商品/a/button/formtable border1 cellspacing0trth编号/thth商品名称/thth是否上架/thth单价/thth备注/thth所属类别/thth操作/th/tr% ResultSet rs (ResultSet)session.getAttribute(set);while(rs.next()){%trth%rs.getString(1) %/thth%rs.getString(2) %/thth%rs.getString(3) %/thth%rs.getString(4) %/thth%rs.getString(5) %/thth%rs.getString(7) %/ththbuttona hrefdel?id%rs.getString(1) % 删除/a/button/th/tr%}%/table/center/body
/html
login.jsp
% page languagejava importjava.util.* pageEncodingutf-8%
%String path request.getContextPath();String basePath request.getScheme() :// request.getServerName() : request.getServerPort() path /;
%!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
html
head
base href%basePath%titleMy JSP index.jsp starting page/title
meta http-equivpragma contentno-cache
meta http-equivcache-control contentno-cache
meta http-equivexpires content0
meta http-equivkeywords contentkeyword1,keyword2,keyword3
meta http-equivdescription contentThis is my page
!--link relstylesheet typetext/css hrefstyles.css--
/headbodyform actionlogin账号input idname namenamebr / 密码input idpwdnamepwdbr / input typesubmit/form
/body
script typetext/javascript srcjquery-3.3.1.js/script
script typetext/javascript$(function() {$(form).submit(function() {var name $(#name).val();var pwd $(#pwd).val();if ( name) {alert(请输入账号)return false;} else if ( pwd) {alert(请输入密码)return false;} else {return true;}})})
/script
/body
/html
我们已经把商品管理系统完整实现了。
扫一扫关注我的公众号获取更多资讯
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/929380.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!