2-21总结
今天练习了csdn里的一个书籍添加代码,大部分是复制粘贴的,仅仅自己看了代码 改掉了一点东西。比之前看的美观点了。
![](https://img2023.cnblogs.com/blog/2914574/202302/2914574-20230221150341073-1310089536.png)
//IntelliJ IDEA //JavaWebProject1 //Book package com.lyb.bean; public class Book { private int id; //编号 private String name; //图书名称 private double price; //价格 private int bookCount; //数量 private String author; //作者 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 double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getBookCount() { return bookCount; } public void setBookCount(int bookCount) { this.bookCount = bookCount; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.SQLException" %> <%@ page import="java.sql.PreparedStatement" %> <html> <head> <title>添加结果</title> <style type="text/css"> body{ background: #d7c7e9; align-content: center; text-align: center; } </style> </head> <body> <%request.setCharacterEncoding("UTF-8");%> <jsp:useBean id="book" class="com.lyb.bean.Book"></jsp:useBean> <jsp:setProperty name="book" property="*"/> <% Connection conn =null; try{ try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); out.print("驱动器加载失败"); } String url = "jdbc:mysql://localhost:3306/demo"; String username = "root"; String password = "135790"; try{ conn = DriverManager.getConnection(url,username,password); }catch (SQLException e){ out.println(e.getMessage()); out.println("数据库连接失败"); } if(conn != null){ String sql = "insert into tb_books(name,price,bookCount,author)values(?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,book.getName()); ps.setDouble(2,book.getPrice()); ps.setInt(3,book.getBookCount()); ps.setString(4,book.getAuthor()); int row = ps.executeUpdate(); if(row>0){ out.println("成功添加了" + row + "条数据!"); } } }catch (Exception e){ e.printStackTrace(); out.print("图书信息添加失败!"); } %> <br> <a href="book.jsp">返回</a> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>图书信息登记</title> <style type="text/css"> body{ background: #d7c7e9; align-items: center; text-align: center; } </style> <script type="text/javascript"> function check(form){ with(form){ if(name.value === ""){ alert("图书名称不能为空"); return false; } if(price.value === ""){ alert("价格不能为空"); return false; } if(bookCount.value === ""){ alert("图书数量不能为空"); return false; } if(author.value === ""){ alert("作者不能为空"); return false; } return true; } } </script> </head> <body> <form action="AddBook.jsp" method="post" onsubmit="return check(this);"> <table align="center" width="450"> <tr> <td align="center" colspan="2"> <h2>添加图书信息</h2> <hr> </td> </tr> <tr> <td align="right">图书名称:</td> <td> <label> <input type="text" name="name"/> </label> </td> </tr> <tr> <td align="right">价格:</td> <td> <label> <input type="text" name="price"/> </label> </td> </tr> <tr> <td align="right">数量:</td> <td> <label> <input type="text" name="bookCount"/> </label> </td> </tr> <tr> <td align="right">作者:</td> <td> <label> <input type="text" name="author"/> </label> </td> </tr> <tr> <td align="center" colspan="2"> <input type="submit" value="提交"> </td> </tr> </table> </form> </body> </html>
运行结果的截图