标签:otf tsp dde except array auth super query index.jsp
package Lei; public class Book { //编号 private int id; //商品名称 private String Name; // 生产厂家 private String Manufacturer; //型号 private String Model; // 规格 private String Specifications; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return Name; } public void setName(String name) { this.Name = name; } public String getManufacturer() { return Manufacturer; } public void setManufacturer(String Manufacturer) { this.Manufacturer=Manufacturer; } public String getModel() { return Model; } public void setModel(String Model) { this.Model=Model; } public String getSpecifications() { return Specifications; } public void setSpecifications(String Specifications) { this.Specifications=Specifications; } }
package Lei; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class DeleteServlet */ @WebServlet("/DeleteServlet") public class DeleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteServlet() { super(); // 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 // 获取商品id int id = Integer.valueOf(request.getParameter("id")); try { // 加载数据库驱动,注册到驱动管理器 Class.forName("com.mysql.jdbc.Driver"); // 数据库连接字符串 String url = "jdbc:mysql://localhost:3306/db_database10?useSSL=false"; // 数据库用户名 String username = "root"; // 数据库密码 String password = "yueyue.1314"; // 创建Connection连接 Connection conn = DriverManager.getConnection(url,username,password); // 删除商品信息的SQL语句 String sql = "delete from tb_books where id=?"; // 获取PreparedStatement PreparedStatement ps = conn.prepareStatement(sql); // 对SQL语句中的第一个占位符赋值 ps.setInt(1, id); // 执行更新操作 ps.executeUpdate(); // 关闭PreparedStatement ps.close(); // 关闭Connection conn.close(); } catch (Exception e) { e.printStackTrace(); } // 重定向到FindServlet response.sendRedirect("FindServlet"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Lei; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; /** * Servlet implementation class FindServlet */ @WebServlet("/FindGetOneServlet") public class FindGetOneServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FindGetOneServlet() { super(); // 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 int id = Integer.valueOf(request.getParameter("id")); try { // 加载数据库驱动,注册到驱动管理器 Class.forName("com.mysql.jdbc.Driver"); // 数据库连接字符串 String url = "jdbc:mysql://localhost:3306/db_database10?useSSL=false"; // 数据库用户名 String username = "root"; // 数据库密码 String password = "yueyue.1314"; // 创建Connection连接 Connection conn = DriverManager.getConnection(url,username,password); // 获取Statement // Statement stmt = conn.createStatement(); // 添加图书信息的SQL语句 // String sql = "select * from tb_books where id=?"; // PreparedStatement ps = conn.prepareStatement(sql, id); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from tb_books where ID="+id); // 对SQL语句中的第一个参数赋值 // ps.setInt(1, id); // 执行查询 // ResultSet rs = ps.executeQuery(sql); // 实例化List对象 List<Book> list = new ArrayList<Book>(); Book book = new Book(); // 判断光标向后移动,并判断是否有效 while(rs.next()){ // 实例化Book对象 // 对id属性赋值 book.setId(rs.getInt("id")); // 对name属性赋值 book.setName(rs.getString("Name")); // 对price属性赋值 book.setManufacturer(rs.getString("Manufacturer")); // 对bookCount属性赋值 book.setModel(rs.getString("Model")); // 对author属性赋值 book.setSpecifications(rs.getString("Specifications")); // 将图书对象添加到集合中 // list.add(book); } // 将图书集合放置到request之中 request.setAttribute("book", book); rs.close(); // 关闭ResultSet // ps.close(); // 关闭Statement conn.close(); // 关闭Connection } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } // 请求转发到book_list.jsp request.getRequestDispatcher("book_update.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Lei; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; /** * Servlet implementation class FindServlet */ @WebServlet("/FindServlet") public class FindServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FindServlet() { super(); // 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 try { // 加载数据库驱动,注册到驱动管理器 Class.forName("com.mysql.jdbc.Driver"); // 数据库连接字符串 String url = "jdbc:mysql://localhost:3306/db_database10?useSSL=false"; // 数据库用户名 String username = "root"; // 数据库密码 String password = "yueyue.1314"; // 创建Connection连接 Connection conn = DriverManager.getConnection(url,username,password); // 获取Statement Statement stmt = conn.createStatement(); // 添加图书信息的SQL语句 String sql = "select * from tb_books"; // 执行查询 ResultSet rs = stmt.executeQuery(sql); // 实例化List对象 List<Book> list = new ArrayList<Book>(); // 判断光标向后移动,并判断是否有效 while(rs.next()){ // 实例化Book对象 Book book = new Book(); // 对id属性赋值 book.setId(rs.getInt("id")); // 对name属性赋值 book.setName(rs.getString("Name")); // 对price属性赋值 book.setManufacturer(rs.getString("Manufacturer")); // 对bookCount属性赋值 book.setModel(rs.getString("Model")); // 对author属性赋值 book.setSpecifications(rs.getString("Specifications")); // 将图书对象添加到集合中 list.add(book); } // 将图书集合放置到request之中 request.setAttribute("list", list); rs.close(); // 关闭ResultSet stmt.close(); // 关闭Statement conn.close(); // 关闭Connection } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } // 请求转发到book_list.jsp request.getRequestDispatcher("book_list.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Lei; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class UpdateServlet */ @WebServlet("/UpdateServlet") public class UpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateServlet() { super(); // 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 response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub int id = Integer.valueOf(request.getParameter("id")); String name=request.getParameter("Name"); String Manufacturer=request.getParameter("Manufacturer"); String Model=request.getParameter("Model"); String Specifications=request.getParameter("Specifications"); // int bookCount = Integer.valueOf(request.getParameter("bookCount")); try { // 加载数据库驱动,注册到驱动管理器 Class.forName("com.mysql.jdbc.Driver"); // 数据库连接字符串 String url = "jdbc:mysql://localhost:3306/db_database10?useSSL=false"; // 数据库用户名 String username = "root"; // 数据库密码 String password = "yueyue.1314"; // 创建Connection连接 Connection conn = DriverManager.getConnection(url,username,password); // 更新SQL语句 String sql = "update tb_books set Name=?,Manufacturer=?,Model=?,Specifications=? where id=?"; // 获取PreparedStatement PreparedStatement ps = conn.prepareStatement(sql); // 对SQL语句中的第一个参数赋值 // ps.setInt(1, bookCount); ps.setString(1, name); // 对SQL语句中的第3个参数赋值 ps.setString(2, Manufacturer); // 对SQL语句中的第4个参数赋值 ps.setString(3,Model); // 对SQL语句中的第5个参数赋值 ps.setString(4, Specifications); // 对SQL语句中的第二个参数赋值 ps.setInt(5, id); // 执行更新操作 ps.executeUpdate(); // 关闭PreparedStatement ps.close(); // 关闭Connection conn.close(); } catch (Exception e) { e.printStackTrace(); } // 重定向到FindServlet response.sendRedirect("FindServlet"); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.PreparedStatement"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <jsp:useBean id="book" class="Lei.Book"></jsp:useBean> <% try { request.setCharacterEncoding("UTF-8"); // int id = Integer.valueOf(request.getParameter("id")); String name=request.getParameter("Name"); String Manufacturer=request.getParameter("Manufacturer"); String Model=request.getParameter("Model"); String Specifications=request.getParameter("Specifications"); // book.setId(id); book.setName(name); book.setManufacturer(Manufacturer); book.setModel(Model); book.setSpecifications(Specifications); // 加载数据库驱动,注册到驱动管理器 Class.forName("com.mysql.jdbc.Driver"); // 数据库连接字符串 String url = "jdbc:mysql://localhost:3306/db_database10?useSSL=false"; // 数据库用户名 String username = "root"; // 数据库密码 String password = "yueyue.1314"; // 创建Connection连接 Connection conn = DriverManager.getConnection(url,username,password); // 添加图书信息的SQL语句 String sql = "insert into tb_books(name,Manufacturer,Model,Specifications) values(?,?,?,?)"; // 获取PreparedStatement PreparedStatement ps = conn.prepareStatement(sql); // 对SQL语句中的第1个参数赋值 // ps.setInt(1,book.getId()); // 对SQL语句中的第2个参数赋值 ps.setString(1, book.getName()); // 对SQL语句中的第3个参数赋值 ps.setString(2, book.getManufacturer()); // 对SQL语句中的第4个参数赋值 ps.setString(3, book.getModel()); // 对SQL语句中的第5个参数赋值 ps.setString(4, book.getSpecifications()); // 执行更新操作,返回所影响的行数 int row = ps.executeUpdate(); // 判断是否更新成功 if(row > 0){ // 更新成功输出信息 out.print("成功添加了 " + row + "条数据!"); } // 关闭PreparedStatement,释放资源 ps.close(); // 关闭Connection,释放资源 conn.close(); } catch (Exception e) { out.print("商品信息添加失败!"); e.printStackTrace(); } %> <br> <a href="index.jsp">返回</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@page import="java.util.List"%> <%@page import="Lei.Book"%> <!DOCTYPE html> <html> <head> <meta charset="GB18030"> <title>所有商品信息</title> </head> <body> <table> <tr bgcolor="white"> <td align="center" colspan="5"> <h2>所有商品信息</h2> </td> </tr> <tr align="center" bgcolor="#e1ffc1" > <td><b>ID</b></td> <td><b>商品名称</b></td> <td><b>生产厂家</b></td> <td><b>型号</b></td> <td><b>规格</b></td> <td><b>操作</b></td> </tr> <% // 获取图书信息集合 List<Book> list = (List<Book>)request.getAttribute("list"); // 判断集合是否有效 if(list == null || list.size() < 1){ out.print("没有数据!"); }else{ // 遍历图书集合中的数据 for(Book book : list){ %> <tr align="center" bgcolor="white"> <td><%=book.getId()%></td> <td><%=book.getName()%></td> <td><%=book.getManufacturer()%></td> <td><%=book.getModel()%></td> <td><%=book.getSpecifications()%></td> <td> <form action="UpdateServlet" method="post" onsubmit="return check(this);"> <input type="hidden" name="id" value="<%=book.getId()%>"> </form> </td> <td> <a href="DeleteServlet?id=<%=book.getId()%>">删除</a> <a href="FindGetOneServlet?id=<%=book.getId()%>">修改</a> </td> </tr> <% } } %> </table> <a href="index.jsp">返回</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@page import="java.util.List"%> <%@page import="Lei.Book"%> <!DOCTYPE html> <html> <head> <meta charset="GB18030"> <title>所有商品信息</title> </head> <body> <form action="UpdateServlet" method="post" onsubmit="return check(this);"> <table> <tr bgcolor="white"> <td align="center" colspan="5"> <h2>所有商品信息</h2> </td> </tr> <tr align="center" bgcolor="#e1ffc1" > <td><b>商品名称</b></td> <td><b>生产厂家</b></td> <td><b>型号</b></td> <td><b>规格</b></td> <td><b>操作</b></td> </tr> <% // 获取图书信息集合 Book book = (Book)request.getAttribute("book"); %> <tr align="center" bgcolor="white"> <td><input type="hidden" name="id" value="<%=book.getId()%>"> <input type="text" name="Name" value="<%=book.getName()%>"> </td> <td><input type="text" name="Manufacturer" value="<%=book.getManufacturer()%>"></td> <td><input type="text" name="Model" value="<%=book.getModel()%>"></td> <td><input type="text" name="Specifications" value="<%=book.getSpecifications()%>"></td> <td> <input type="hidden" name="id" value="<%=book.getId()%>"> </td> <td> <input type="submit" value="保存"> </td> </tr> </table> </form> <a href="index.jsp">返回</a> </body> <script type="text/javascript"> function check(form){ with(form){ if(Name.value == ""){ alert("商品名称不能为空"); return false; } if(Manufacturer.value == ""){ alert("生产厂家不能为空"); return false; } if(Model.value == ""){ alert("型号不能为空"); return false; } if(Specifications.value == ""){ alert("规格不能为空"); return false; } return true; } } </script> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加数据</title> <script type="text/javascript"> function check(form){ with(form){ if(Name.value == ""){ alert("商品名称不能为空"); return false; } if(Manufacturer.value == ""){ alert("生产厂家不能为空"); return false; } if(Model.value == ""){ alert("型号不能为空"); return false; } if(Specifications.value == ""){ alert("规格不能为空"); return false; } return true; } } </script> </head> <body> <form action="AddBook.jsp" method="post" onsubmit="return check(this);"> <table> <tr> <td align="center" colspan="2"> <h2>添加库存商品信息</h2> <hr> </td> </tr> <!-- <td align="right">id:</td> --> <!-- <td><input type="text" name="id" /></td> --> <!-- <tr> --> <td align="right">商品名称:</td> <td><input type="text" name="Name" /></td> </tr> <tr> <td align="right">生产厂家:</td> <td><input type="text" name="Manufacturer" /></td> </tr> <tr> <td align="right">商品型号:</td> <td><input type="text" name="Model" /></td> </tr> <tr> <td align="right">规 格:</td> <td><input type="text" name="Specifications" /></td> </tr> <tr> <td align="center" colspan="2"> <input type="submit" value="添 加"> </td> </tr> </table> </form> <a href="FindServlet">查看所有商品</a> </body> </html>
标签:otf tsp dde except array auth super query index.jsp
原文地址:https://www.cnblogs.com/muailiulan/p/10116980.html