商品信息录入
1、录入页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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 bgcolor="#00CC33"> <div style=heigh:800px;weight:300px;position:absolute;left:200px;top:100px;background-color:#OF3> <form action="shangpin" method="post"> <table heigh="800" weight="300" cellpadding="0" cellspacing="0"> <tr align="center" height="50"><td colspan="2"><font size="6">     请输入商品信息</font></td></tr> <tr ><td weight="40" align="right">商品编号:</td><td><input type="text" name="spbh" ></td></tr> <tr><td weight="40" align="right">商品名称:</td><td ><input type="text" name="spm"></td></tr> <tr><td weight="40" align="right">生产日期:</td><td><input type="text" name="scrq"></td></tr> <tr><td weight="40" align="right">保质期:</td><td><input type="text" name="bzq"></td></tr> <tr><td weight="40" align="right">生产厂家:</td><td><input type="text" name="cs"></td></tr> <tr align="center"><td colspan="2">     <input type="submit" value="提交数据"></td></tr> </table> </form> </div> </body> </html> </body> </html>
2、更改页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <!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> <%try{ //1、连接数据库 Class.forName("oracle.jdbc.driver.OracleDriver"); //(2)得到链接 Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCLER","test","asd123"); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select * from shangpin"); //3、输出结果集 if(rs!=null) { while(rs.next()) { out.print("商品编号:"+rs.getString("spbn")+" "); out.print("商品名称:"+rs.getString("spm")+" "); try{ out.print("生产日期:"+rs.getString("scrq")+" "); }catch(Exception e){} out.print("保质期:"+rs.getDate("bzq") +" "); out.print( "生产厂家:"+rs.getString("cs")+"<a href='caozuosj?spbh="+rs.getString("spbh")+"'>【删除】</a><a href='Update.jsp?spbh="+rs.getString("spbh")+"'>【修改】</a><br> "); } rs.close(); } } catch(Exception e) { e.printStackTrace(); } %> </body> </html>
3、过滤器
package lianxi77; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * Servlet Filter implementation class zhuanhuan */ public class zhuanhuan implements Filter { /** * Default constructor. */ public zhuanhuan() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
4、数据插入
package lianxi77; import java.io.IOException; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.text.SimpleDateFormat; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class shangpin extends HttpServlet { private static final long serialVersionUID = 1L; public shangpin() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收数据 String spbh=request.getParameter("spbh"); String spm=request.getParameter("spm"); String scrq=request.getParameter("scrq"); String bzq=request.getParameter("bzq"); String cs=request.getParameter("cs"); if(spbh!=null&&spbh.trim().length()!=0) { try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCLER","test","asd123"); System.out.println("lainjiechenggong"); PreparedStatement pst=conn.prepareStatement( "insert into shangpin(spbh,spm,scrq,bzq,cs)" +" values(?,?,?,?,?)"); SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd"); java.util.Date b=sd.parse(scrq); Date da=new Date(b.getTime()); pst.setString(1, spbh); pst.setString(2, spm); pst.setDate(3, da); pst.setString(4, bzq); pst.setString(5, cs); pst.executeUpdate(); pst.close(); conn.close(); response.getWriter().println("保存成功!"); response.setHeader("refresh","3;URL=caozuo.jsp"); } catch(Exception e) { e.printStackTrace(); } } else { response.getWriter().println("请正确提交数据!"); response.setHeader("refresh","3;URL=shuruye.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
5、数据更改
package lianxi77; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class caozuosj extends HttpServlet { private static final long serialVersionUID = 1L; public caozuosj() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String num=request.getParameter("spbm"); if(num!=null&&num.trim().length()!=0) { try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCLER","test","asd123"); PreparedStatement pst=conn.prepareStatement( "delete shangpin where"+" spbm=?"); pst.setString(1, num); pst.executeUpdate(); pst.close(); conn.close(); response.getWriter().println("删除成功!"); response.setHeader("refresh","3;URL=caozuo.jsp"); } catch(Exception e) { e.printStackTrace(); response.getWriter().println("删除失败!"); response.setHeader("refresh","3;URL=caozuo.jsp"); } } else { response.getWriter().println("请正确提交数据!"); response.setHeader("refresh","3;URL=suruye.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }