图书管理系统项目

第一天:准备把整个框架顺一遍,第一次独立写这个算是我人生中第一个java小项目,不清楚多少天能完成,我会坚持把自己的心得写在这里,主要是给自己看(不是学文的,文笔很差见谅,争取把注释写详细点),如果能帮助其他初学者更好了  一起加油!

点击打开链接

DriverManager.getConnection(String url,String user,String password)  我之前不太理解 特意查了下API

图书管理系统项目(初学者必看的小项目) 图书管理系统项目(初学者必看的小项目)

第一天搭建一个数据库实现基本功能  (因为前三步都需要连接数据库,定义一个数据库连接)
在类中创建一个构造函数初始化数据库的连接 
(PS: 为什么要创建数据库,为了初始化数据库,在后面会经常用的到)
1.创建数据库  createDatabase()   
2.创建表        createTable()
3.创建文件     addData(String filename)

我标记红色的请留意

[html] view plain copy
  1. package javastudy;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.sql.DriverManager;  
  7. import java.sql.PreparedStatement;  
  8. import java.sql.SQLException;  
  9.   
  10. import com.mysql.jdbc.Connection;  
  11. import com.mysql.jdbc.Statement;  
  12.   
  13. public class DBLib {  
  14.     //因为三个方法都需要连接数据库  
  15.     Connection conn;   //定义一个变量  
  16.     Statement st;        
  17.     public DBLib() throws ClassNotFoundException//定义一个构造函数 初始化数据库连接   
  18. , SQLException  
  19.     {  
  20.         Class.forName("com.mysql.jdbc.Driver");  //加载驱动程序  
  21.         //需要得到三个参数  
  22.         String url="jdbc:mysql://127.0.0.1:3306<span style="color:rgb(255,0,0)">?useUnicode=true&characterEncoding=utf-8</span>";//本机ip地址 本机端口  <span style="color:#ff0000">防止数据库乱码(若无乱码无须添加)</span>  
  23.         String user="root";           //数据库的名字  
  24.         String pwd="521125";          //数据库密码  
  25.         conn=(Connection) DriverManager.getConnection(url, user, pwd); //初始化数据库  拿到三个参数  
  26.         <span style="color:#ff0000">st=(Statement) conn.createStatement();</span>   //创建一个 Statement 对象来将 SQL 语句发送到数据库。  
  27.     }  
  28.     public void createDatabase() throws SQLException   //创建一个数据库  
  29.     {  
  30.         String sql="drop database if exists Book;";    //判断数据库存在删除  
  31.         st.executeUpdate(sql);//执行给定 SQL 语句  
  32.         sql="create database Book;";   //创建表  
  33.         st.executeUpdate(sql);//执行  
  34.               
  35.           
  36.     }  
  37.     public void createTable() throws SQLException     //创建一个表  
  38.     {  
  39.         String sql="use Book;";//使用表  
  40.         <span style="color:#ff0000">st.executeQuery(sql);//执行</span>  
  41.         sql="create table BOOKS";   
  42.         sql+="(";  
  43.         sql+="  ID int(4) not null primary key auto_increment,";  
  44.         sql+="  Name    varchar(100),";  
  45.         sql+="  Author varchar(50),";  
  46.         sql+="  Price  decimal,";  
  47.         sql+="Publisher varchar(100)";  
  48.         sql+=") <span style="font-family:SimHei; color:#ff0000">ENGINE=InnoDB DEFAULT CHARSET=utf8;</span>";//我额外加的 因为我安装数据库<span style="color:#ff0000">未设置UTF-8 出现乱码</span>  
  49.         st.executeUpdate(sql);//执行语句  
  50.     }  
  51.     public void addData(String filename) throws SQLException//创建一个文档  
  52. , IOException  
  53.     {  
  54.         String sql="use Book;";//使用表  
  55.         st.executeUpdate(sql);//执行  
  56.         FileReader fr=new FileReader(filename);      //读取  
  57.         BufferedReader br=new BufferedReader(fr);   //缓冲区(包装类)  
  58.         String line;  
  59.         sql=" INSERT INTO BOOKS (Name,Author,Price,Publisher) VALUES(?,?,?,?)";  //sql语句  
  60.         PreparedStatement ps=conn.prepareStatement(sql);  
  61.           
  62.         while((line=br.readLine())!=null)   //判断是否读完  
  63.         {  
  64.             String[] values=line.split(",");                //以逗号拆分 用一个字符串数组接收            
  65.             ps.setString(1, values[0]);                     //字段从1数组从0开始  
  66.             ps.setString(2, values[1]);  
  67.             <span style="color:#ff0000">ps.setDouble(3, Double.parseDouble(values[2]));</span>//把字符串强制 转换Double  
  68.             ps.setString(4, values[3]);  
  69.             st.executeUpdate(sql);//执行  
  70.         }  
  71.         br.close();             //关闭资源  
  72.     }  
  73. }  

2.创建一个init  初始化测试创建数据库是否成功

第一个问题:

遇到一个错误 创建数据实现,无法向里面加入数据,显示代码错误 

图书管理系统项目(初学者必看的小项目)

解决方法:

由于粗心,和对代码的不熟悉浪费了十分钟去查错误,特意去看了API把
1.executeUpdate()  2.executeQuery();

图书管理系统项目(初学者必看的小项目)

图书管理系统项目(初学者必看的小项目)图书管理系统项目(初学者必看的小项目)

上面错误代码我没有修改!复制时需要注意

第二个问题 数据库在创建时 生成的不是utf-8 

图书管理系统项目(初学者必看的小项目)

解决方法1:

第3个问题 导入文字出现乱码 

解决方法1:去根目录找文件把属性改成utf-8   C:\Users\Administrator\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\图书管理项目 (未成功)

解决方案2:去官网找了一篇大佬的资料 

图书管理系统项目(初学者必看的小项目)

 jsp用来显示结果,用来做页面呈现
 逻辑处理用Servlet来做。
图书管理系统项目(初学者必看的小项目)
 jsp:用来显示结果

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8" import="javastudy.*"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>   
  8. <script type="text/javascript" src="pytjon/jquery-1.11.1.js"></script>  
  9. <script type="text/javascript">  
  10. $(function(){  
  11.     $("input[type='button']").click(function(data){               //当按钮被点击  
  12.     $.post("initDB.jsp",function(data){                           //进入initDB。jsp  
  13.             $("#info").html(data);  
  14.         });   
  15.     });   
  16. });  
  17. </script>  
  18.   
  19. </head>  
  20. <body>  
  21. <input type="button" value="初始化数据">  
  22. <div id="info"></div>  
  23. </body>  

 servlet:处理逻辑,处理业务,

[html] view plain copy
  1. package javastudy;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.sql.SQLException;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.annotation.WebServlet;  
  9. import javax.servlet.http.HttpServlet;  
  10. import javax.servlet.http.HttpServletRequest;  
  11. import javax.servlet.http.HttpServletResponse;  
  12.   
  13. /**  
  14.  * Servlet implementation class InitIt  
  15.  */  
  16. @WebServlet("/InitIt")  
  17. public class InitIt extends HttpServlet {  
  18.     private static final long serialVersionUID = 1L;  
  19.          
  20.     /**  
  21.      * @see HttpServlet#HttpServlet()  
  22.      */  
  23.     public InitIt() {  
  24.         super();  
  25.         // TODO Auto-generated constructor stub  
  26.     }  
  27.   
  28.     /**  
  29.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)  
  30.      */  
  31.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  32.         // TODO Auto-generated method stub  
  33.         //response.getWriter().append("Served at: ").append(request.getContextPath());  
  34.   
  35.         DBLib lib;  
  36.         try {  
  37.             lib = new DBLib();  
  38.             lib.createDatabase();  
  39.             lib.createTable();  
  40.             lib.addData(this.getServletContext().getRealPath("") + "\\data\\data.txt");  
  41.             response.setCharacterEncoding("utf-8");  
  42.             PrintWriter out=response.getWriter();  
  43.             out.print("初始化成功!");  
  44.         } catch (ClassNotFoundException | SQLException e) {  
  45.             // TODO Auto-generated catch block  
  46.             e.printStackTrace();  
  47.         }  
  48.     }  
  49.   
  50.     /**  
  51.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)  
  52.      */  
  53.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  54.         // TODO Auto-generated method stub  
  55.         doGet(request, response);  
  56.     }  
  57.   
  58. }  
[html] view plain copy
  1. Servlet解决乱码的方法  
  2.   
  3. response.setContentType("text/html;charset=utf-8");  
  4. response.setCharacterEncoding("utf-8");  
  5.   
  6. 注解配置Servlet  
  7. Annotation=注解  
  8.   
  9.   
  10.     web.xml配置servlet的方法  
  11.   
  12.     <servlet>    
  13.             <servlet-name>a</servlet-name>    
  14.             <servlet-class>javastudy.InitIt</servlet-class>    
  15.     </servlet>    
  16.     <servlet-mapping>    
  17.             <servlet-name>a</servlet-name>    
  18.             <url-pattern>/myservlet</url-pattern>    
  19.      </servlet-mapping>    

前面的数据库创建测试通过,数据库成功搭建

把整个项目步骤写一下:

JSP网站开发模式二:JSP+JavaBeans+Servlet Model II MVC的思想(Model=模型,View=视图,Control=控制)
  将控制业务逻辑的代码写到Servlet中,讲页面的展现(HTML)和业务逻辑的控制(Java)分离开来。


步骤:1.建立一个JavaBean,它是一个类Book

[html] view plain copy
  1. package javastudy;  
  2.   
  3. public class Book {  
  4.     int id;  
  5.     String name;  
  6.     String author;  
  7.     float price;  
  8.     String publisher;  
  9.     public int getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(int id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.     public String getAuthor() {  
  22.         return author;  
  23.     }  
  24.     public void setAuthor(String author) {  
  25.         this.author = author;  
  26.     }  
  27.     public float getPrice() {  
  28.         return price;  
  29.     }  
  30.     public void setPrice(float price) {  
  31.         this.price = price;  
  32.     }  
  33.     public String getPublisher() {  
  34.         return publisher;  
  35.     }  
  36.     public void setPublisher(String publisher) {  
  37.         this.publisher = publisher;  
  38.     }  
  39. }  


      2.建立一类叫做BookDAO ,这是数据访问层DAO=Data Access Object=数据访问接口连接数据库,实现以下方法。

    2.1BookDAO()

[html] view plain copy
  1. Connection conn;   //定义公共变量  
  2.     Statement st;     
  3.     String sql;   
  4.     PreparedStatement ps;  
  5.     public BookDAO() throws ClassNotFoundException,SQLException//定义一个构造函数 初始化数据库连接  
  6.     {  
  7.     Class.forName("com.mysql.jdbc.Driver");  //加载驱动程序  
  8.         //需要得到三个参数  
  9.         String url="jdbc:mysql://127.0.0.1:3306/book?useUnicode=true&characterEncoding=utf-8";//本机ip地址 本机端口  防止数据库乱码  
  10.         String user="root";           //数据库的名字  
  11.         String pwd="521125";          //数据库密码  
  12.         conn=DriverManager.getConnection(url,user,pwd); //初始化数据库  拿到三个参数  
  13.         st=conn.createStatement();    //创建一个 Statement 对象来将 SQL 语句发送到数据库。  
  14.           
  15.     }  

 2.2 addBook(Book book)

[html] view plain copy
  1. //增加  
  2.         public int addBook(Book book) throws Exception  
  3.         {  
  4.             sql="insert into BOOKS(name,author,price,publisher) values(?,?,?,?)";  
  5.             ps=conn.prepareStatement(sql);//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。  
  6.             ps.setString(1,book.getName());  
  7.             ps.setString(2,book.getAuthor());  
  8.             ps.setDouble(3,book.getPrice());  
  9.             ps.setString(4,book.getPublisher());  
  10.               
  11.             return ps.executeUpdate();  
  12.         }  

 2.3 deleteBook(int id)

[html] view plain copy
  1. //删除  
  2.         public int deleteBook(int id) throws Exception //通过ID查找  
  3.         {  
  4.             sql="delete from books where id=?";               
  5.             ps=conn.prepareStatement(sql);  
  6.             ps.setInt(1, id);  
  7.             return ps.executeUpdate();  
  8.             // 在此 PreparedStatement 对象中执行 SQL 语句,该语句必须是一个 SQL 数据操作语言语句,或者是无返回内容的 SQL 语句  
  9.         }  
  2.4 ArrayList<Book> getBooks()
[html] view plain copy
  1. //拿到所有 的图书  
  2.         public ArrayList<Book> getbooks() throws Exception {  
  3.             sql="select * from books";  
  4.             ps=conn.prepareStatement(sql);  
  5.             ResultSet rs = ps.executeQuery();  
  6.             ArrayList<Book> list=new ArrayList<Book>();  
  7.               
  8.             while(rs.next()) {  
  9.                 Book book=new Book();  
  10.                 book.setId(rs.getInt(1));  
  11.                 book.setName(rs.getString(2));  
  12.                 book.setAuthor(rs.getString(3));  
  13.                 book.setPrice(rs.getFloat(4));   
  14.                 book.setPublisher(rs.getString(5));  
  15.                 list.add(book);  
  16.             }  
  17.             return list;  
  18.         }  
  2.5 modifyBook(int id,Book newbook)

[html] view plain copy
  1. //修改一本书  
  2.     public int modifyBook(int id, Book newbook) throws Exception {  
  3.         // TODO Auto-generated method stub  
  4.         sql="update books set name=?,author=?,price=?,publisher=? where id=?";  
  5.         ps=conn.prepareStatement(sql);  
  6.         ps.setString(1, newbook.getName());  
  7.         ps.setString(2, newbook.getAuthor());  
  8.         ps.setDouble(3, newbook.getPrice());  
  9.         ps.setString(4, newbook.getPublisher());  
  10.         ps.setInt(5, id);  
  11.         return ps.executeUpdate();    
  12.     }  
  2.6getBookById(int id)
[html] view plain copy
  1. //通过一本书得到信息  
  2.          public Book getBookById(int id) throws SQLException  
  3.         {  
  4.             String sql="select * from books where id=?";  
  5.             ps=conn.prepareStatement(sql);  
  6.             ps.setInt(1, id);  
  7.             ResultSet rs=ps.executeQuery();  
  8.             rs.next();  
  9.             Book book=new Book();  
  10.             book.setId(rs.getInt(1));  
  11.             book.setName(rs.getString(2));  
  12.             book.setAuthor(rs.getString(3));  
  13.             book.setPrice(rs.getFloat(4));  
  14.             book.setPublisher(rs.getString(5));  
  15.             return book;  
  16.               
  17.                       
  18.         }  
 2.7getBooks(String bookName)

[html] view plain copy
  1. <span style="white-space:pre">      </span>//搜索图书  
  2. <span style="white-space:pre">      </span>public ArrayList<Book> getBook(String books) throws Exception {  
  3. <span style="white-space:pre">      </span>//<span style="white-space:pre"> </span>SELECT * FROM users WHERE username LIKE '%三%'  
  4. <span style="white-space:pre">          </span>//sql="select * from books where name=?";  
  5. <span style="white-space:pre">          </span>//select * from table_name where id like '83%58193';  
  6. <span style="white-space:pre">          </span>//sql:select id,name from tablename where contains( address, '上海' );  
  7. <span style="white-space:pre">      </span>/*<span style="white-space:pre"> </span>SELECT student_id,student_name  
  8. <span style="white-space:pre">          </span>FROM students  
  9. <span style="white-space:pre">          </span>WHERE CONTAINS( address, 'beijing' )*/  
  10. <span style="white-space:pre">      </span>   
  11. <span style="white-space:pre">          </span>sql="SELECT * FROM books WHERE name LIKE '%?%'";  
  12. <span style="white-space:pre">          </span>ps=conn.prepareStatement(sql);  
  13. <span style="white-space:pre">          </span>ps.setString(1, books);  
  14. <span style="white-space:pre">          </span>ResultSet rs = ps.executeQuery(sql);  
  15. <span style="white-space:pre">          </span>ArrayList<Book> list=new ArrayList<Book>();  
  16. <span style="white-space:pre">          </span>while(rs.next()) {  
  17. <span style="white-space:pre">              </span>Book book=new Book();  
  18. <span style="white-space:pre">              </span>book.setId(rs.getInt(1));  
  19. <span style="white-space:pre">              </span>book.setName(rs.getString(2));  
  20. <span style="white-space:pre">              </span>book.setAuthor(rs.getString(3));  
  21. <span style="white-space:pre">              </span>book.setPrice(rs.getFloat(4));   
  22. <span style="white-space:pre">              </span>book.setPublisher(rs.getString(5));  
  23. <span style="white-space:pre">              </span>list.add(book);  
  24. <span style="white-space:pre">          </span>}  
  25. <span style="white-space:pre">          </span>return list;  
  26. <span style="white-space:pre">      </span>}       

搜索功能待更新 

3.做一个jsp页面,bookList.jsp。

    整体给大家看一下:

图书管理系统项目(初学者必看的小项目)
  <input type="hidden" name="id">  隐藏 
   booklist.jsp,页面加载时,列表显示所有的图书
    3.1【样式表+Js代码】
[html] view plain copy
  1. <script type="text/javascript">  
  2.   
  3. function altRows(id){  
  4.     if(document.getElementsByTagName){    
  5.           
  6.         var table = document.getElementById(id);    
  7.         var rows = table.getElementsByTagName("tr");   
  8.            
  9.         for(i = 0; i < rows.length; i++){            
  10.             if(i % 2 == 0){  
  11.                 rows[i].className = "evenrowcolor";  
  12.             }else{  
  13.                 rows[i].className = "oddrowcolor";  
  14.             }        
  15.         }  
  16.     }  
  17. }  
  18.   
  19. window.onload=function(){  
  20.     altRows('gridtable');  
  21. }  
  22. </script>  
[html] view plain copy
  1. <style type="text/css">  
  2.   
  3.   
  4. table.gridtable {  
  5. <span style="white-space:pre">  </span>font-family: verdana,arial,sans-serif;  
  6. <span style="white-space:pre">  </span>font-size:11px;  
  7. <span style="white-space:pre">  </span>color:#333333;  
  8. <span style="white-space:pre">  </span>border-width: 1px;  
  9. <span style="white-space:pre">  </span>border-color: #a9c6c9;  
  10. <span style="white-space:pre">  </span>border-collapse: collapse;  
  11. }  
  12. table.gridtable th {  
  13. <span style="white-space:pre">  </span>border-width: 1px;  
  14. <span style="white-space:pre">  </span>padding: 8px;  
  15. <span style="white-space:pre">  </span>border-style: solid;  
  16. <span style="white-space:pre">  </span>border-color: #a9c6c9;  
  17. }  
  18. table.gridtable td {  
  19. <span style="white-space:pre">  </span>border-width: 1px;  
  20. <span style="white-space:pre">  </span>padding: 8px;  
  21. <span style="white-space:pre">  </span>border-style: solid;  
  22. <span style="white-space:pre">  </span>border-color: #a9c6c9;  
  23. }  
  24. .oddrowcolor{  
  25. <span style="white-space:pre">  </span>background-color:#d4e3e5;  
  26. }  
  27. .evenrowcolor{  
  28. <span style="white-space:pre">  </span>background-color:#c3dde0;  
  29. }  
  30. *{margin:0;padding:0;}  
  31. <span style="white-space:pre">          </span>body{overflow:hidden;background:url("images/2.jpg");}  
  32. </style>  
    3.2【增】页面有一个增加图书按钮,可以增加图书

图书管理系统项目(初学者必看的小项目)

[html] view plain copy
  1. $("#btnAdd").click(function(){  
  2.     location.href="add.jsp";  
  3. });  
[html] view plain copy
  1. <embed src="music/4.mp3" width="0.01" height="0.01"></embed>  
  2.      <form method="post" action="AddIt">  
  3.      <table class="gridtable" id="gridtable">  
  4.      <tr><td colspan="2">添加图书信息</td></tr>  
  5.      <tr><td>名称</td><td><input type="text" name="name" ></td></tr>  
  6.      <tr><td>作者</td><td><input type="text" name="author" ></td></tr>  
  7.      <tr><td>价格</td><td><input type="text" name="price" ></td></tr>  
  8.      <tr><td>出版社</td><td><input type="text" name="publisher"></td></tr>  
  9.      <tr><td colspan="2"><input type="submit" value="添加图书信息"><div id="info">     </div></td></tr>  
  10. </table>  
  11. </form>  
  12. </body>  
[html] view plain copy
  1. response.setCharacterEncoding("utf-8");  
  2.         request.setCharacterEncoding("utf-8");  
  3.         //int id=Integer.parseInt(request.getParameter("id"));  //得到ID  
  4.         String name=request.getParameter("name");  
  5.         String author=request.getParameter("author");  
  6.         float price=Float.parseFloat(request.getParameter("price"));  
  7.         String publisher=request.getParameter("publisher");  
  8.         Book newbook=new Book();  
  9.         newbook.name=name;  
  10.         newbook.author=author;  
  11.         newbook.price=price;  
  12.         newbook.publisher=publisher;  
  13.         BookDAO dao;  
  14.         try {  
  15.             dao=new BookDAO();  
  16.             dao.addBook(newbook);  
  17.             response.sendRedirect("BookList.jsp");  
  18.         } catch (Exception e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         }  
  22.       
    3.2【删】页面上有一个删除按钮,可以删除图书

图书管理系统项目(初学者必看的小项目)

[html] view plain copy
  1. $("#btnDelete").click(function(){  
  2.     //将被选中的图书的id收集起来  
  3.     var ids="";  
  4.     var i=0;  
  5.     $("input[type='checkbox'][name='selectit']").each(function(index, element) {  
  6.         if(index>0)  
  7.         {  
  8.             if($(this).prop("checked"))  
  9.             {  
  10.                 var id= $(this).parent().parent().children()  
  11.                     .eq(0).children().eq(0).val();  
  12.                 ids+=id;  
  13.                 i++;  
  14.                 if(!$(this).is(":last"))  
  15.                 {  
  16.                     ids+=",";  
  17.                 }  
  18.             }  
  19.         }  
  20.     });  
  21.     var answer=confirm("你确定要删除选定的这" +  i+ "本图书吗?");  
  22.     if(!answer)  
  23.     {  
  24.         return;  
  25.     }  
  26.     location.href="DeleteIt?ids=" + ids;  
  27. });  
[html] view plain copy
  1. response.setCharacterEncoding("utf-8");  
  2. <span style="white-space:pre">      </span>String ids=request.getParameter("ids");  
  3. <span style="white-space:pre">      </span>String [] id=ids.split(",");  
  4. <span style="white-space:pre">  </span>  
  5. <span style="white-space:pre">      </span>  
  6. <span style="white-space:pre">          </span>try {  
  7. <span style="white-space:pre">              </span>BookDAO dao =new BookDAO();  
  8. <span style="white-space:pre">              </span>for(int i=0;i<id.length;i++)  
  9. <span style="white-space:pre">              </span>{  
  10. <span style="white-space:pre">              </span>dao.deleteBook(Integer.parseInt(id[i]));  
  11. <span style="white-space:pre">              </span>}  
  12. <span style="white-space:pre">          </span>} catch (Exception e) {  
  13. <span style="white-space:pre">              </span>// TODO Auto-generated catch block  
  14. <span style="white-space:pre">              </span>e.printStackTrace();  
  15. <span style="white-space:pre">          </span>}  
  16. <span style="white-space:pre">      </span>response.sendRedirect("BookList.jsp");  
  17. <span style="white-space:pre">      </span>  
  18. <span style="white-space:pre">      </span>  
  19. <span style="white-space:pre">      </span>  
  20. <span style="white-space:pre">      </span>//response.getWriter().append("Served at: ").append(request.getContextPath());  
  21. <span style="white-space:pre">  </span>}  
    3.3【改】每本图书后面有一个修改按钮,可以修改图书

图书管理系统项目(初学者必看的小项目)

[html] view plain copy
  1. $("input[type='button'][name='modifyit']").click(function(){  
  2.         //获取id  
  3.         var id= $(this).parent().parent().children()  
  4.         .eq(0).children().eq(0).val();  
  5.         //页面跳转  
  6.         location.href="modify.jsp?id="+id;  
  7.     });   
[html] view plain copy
  1. <body>  
  2. <embed src="music/3.mp3" width="0.01" height="0.01"></embed>  
  3. <%    
  4.  int id=Integer.parseInt(request.getParameter("id"));  //需要封装   
  5.  BookDAO dao=new BookDAO();  
  6.  <span style="white-space:pre">    </span>Book book=dao.getBookById(id);  //得到ID  
  7.   
  8.   
  9. %>  
  10. <form method="post" action="Modify">  
  11. <table class="gridtable" id="gridtable" >  
  12. <tr><td colspan="2">修改图书信息</td></tr>  
  13.   <span style="white-space:pre">   </span><tr><td><input type="hidden" name="id" value="<%=book.getId()%>"></td></tr>  
  14.     <tr><td>名称</td><td><input type="text" name="name"value="<%=book.getName() %>"></td></tr>  
  15.     <tr><td>作者</td><td><input type="text" name="author"value="<%=book.getAuthor() %>"></td></tr>  
  16.     <tr><td>价格</td><td><input type="text" name="price"value="<%=book.getPrice() %>"></td></tr>  
  17.     <tr><td>出版社</td><td><input type="text" name="publisher"value="<%=book.getPublisher() %>"></td></tr>  
  18.   <tr><td colspan="2"><input type="submit" value="修改图书信息"><div id="info"></div></td></tr>  
  19. </table>  
  20.   
  21.   
  22. </form>  
  23. </body>  
[html] view plain copy
  1. response.setCharacterEncoding("utf-8");  
  2. <span style="white-space:pre">      </span>request.setCharacterEncoding("utf-8");  
  3. <span style="white-space:pre">      </span>int id=Integer.parseInt(request.getParameter("id"));  //得到ID  
  4. <span style="white-space:pre">      </span>String name=request.getParameter("name");  
  5. <span style="white-space:pre">      </span>String author=request.getParameter("author");  
  6. <span style="white-space:pre">      </span>float price=Float.parseFloat(request.getParameter("price"));  
  7. <span style="white-space:pre">      </span>String publisher=request.getParameter("publisher");  
  8. <span style="white-space:pre">      </span>Book newbook=new Book();  
  9. <span style="white-space:pre">      </span>newbook.name=name;  
  10. <span style="white-space:pre">      </span>newbook.author=author;  
  11. <span style="white-space:pre">      </span>newbook.price=price;  
  12. <span style="white-space:pre">      </span>newbook.publisher=publisher;  
  13. <span style="white-space:pre">      </span>BookDAO dao;  
  14. <span style="white-space:pre">      </span>  
  15. <span style="white-space:pre">      </span>  
  16. <span style="white-space:pre">      </span>  
  17. <span style="white-space:pre">      </span>try {dao=new BookDAO();  
  18. <span style="white-space:pre">          </span>dao.modifyBook(id,newbook);  
  19. <span style="white-space:pre">      </span>} catch (Exception e) {  
  20. <span style="white-space:pre">          </span>// TODO Auto-generated catch block  
  21. <span style="white-space:pre">          </span>e.printStackTrace();  
  22. <span style="white-space:pre">      </span>}  
  23. <span style="white-space:pre">      </span>response.sendRedirect("BookList.jsp");  
  24. <span style="white-space:pre">      </span>  
  25. <span style="white-space:pre">      </span>//response.getWriter().append("Served at: ").append(request.getContextPath());  
  26. <span style="white-space:pre">  </span>}  
 3.4【查】页面上有一个选中按钮,可以根据图书的是否被选中查询显示图书

图书管理系统项目(初学者必看的小项目)图书管理系统项目(初学者必看的小项目)

[html] view plain copy
  1. $("#query").click(function(){  
  2.     //将被选中的图书的id收集起来  
  3. /*  var ids="";  
  4.     var i=0;  
  5.     var   
  6.     $("input[type='checkbox'][name='selectit']").each(function(index, element) {  
  7.         if(index>0)  
  8.         {  
  9.             if($(this).prop("checked"))  
  10.             {  
  11.                 var id= $(this).parent().parent().children()  
  12.                     .eq(0).children().eq(0).val();  
  13.                 ids+=id;  
  14.                 i++;  
  15.                 if(!$(this).is(":last"))  
  16.                 {  
  17.                     ids+=",";  
  18.                 }  
  19.             }  
  20.         }  
  21.     });  
  22.     */  
  23.     var arr=[];  
  24.     $("input[type='checkbox']:checked").each(function(){   
  25.          arr.push(this.value);  
  26.     })  
  27.     var json = JSON.stringify(arr);//数组转换成json,都在了,数组和json  
  28.     alert(json + arr);  
  29.   
  30.     var answer=confirm("你确定要查询选定的这" +  arr.length + "本图书吗?");  
  31.     if(!answer)  
  32.     {  
  33.         return;  
  34.     }  
  35.     location.href="reveal.jsp?ids="+arr;  
  36. });  
     无须处理 只需要判断选中多少 遍历出来 没用到Servlet


3.5【搜】页面上有一个搜索按钮,可以根据图书的名称和作者,来搜索并显示图书
图书管理系统项目(初学者必看的小项目)图书管理系统项目(初学者必看的小项目)
(待)修改

[html] view plain copy
  1. $("#search").click(function(){  
  2.     location.href="search.jsp";  
  3. });  
[html] view plain copy
  1. <body>  
  2. <center>  
  3.  <marquee width="300" direction=left height="50"><center><font color="pink" size="7" face="华文彩云"><b>图书信息</b></font></center></marquee>  
  4. <embed src="music/3.mp3" width="0.01" height="0.01"></embed>  
  5. <table class="gridtable" id="gridtable" height="400" width="30%">  
  6.      <%    
  7.     request.setCharacterEncoding("UTF-8");  
  8.     response.setCharacterEncoding("UTF-8");  
  9.     String name=request.getParameter("name");   
  10.     String condition=request.getParameter("condition");   
  11.     out.print(name);  
  12.   
  13.     %>     
  14.     <%     
  15.         BookDAO dao=new BookDAO();  
  16.         ArrayList<Book> list=dao.getbooks();  
  17.         for(int i=0;i<list.size();i++){  
  18.             Book book=list.get(i);  
  19.     %>     
  20.     <tr>  
  21.     <td><%=i+1 %><input type="hidden" name="id" value="<%=book.getId()%>"></td>  
  22.     <td><h3><%=book.getName() %></h3></td>  
  23.     <td><h3><%=book.getAuthor() %></h3></td>  
  24.     <td><h3><%=book.getPrice() %></h3></td>  
  25.     <td><h3><%=book.getPublisher() %></h3></td>  
  26.     </tr>  
  27.     <%  
  28.         }  
  29.     %>  
  30.   
  31. </table>  
  32. </center>  
  33. </body>  

创建一个BookDAO的类实现数据库的基本功能 增删改查,新建个类aaa做测试
[html] view plain copy
  1. package javastudy;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. public class aaa {  
  6.   
  7.     public static void main(String[] args) throws Exception {  
  8.         // TODO Auto-generated method stub  
  9.         BookDAO dao=new BookDAO();  
  10.         ArrayList<Book> list = dao.getbooks();  
  11.         for (Book book : list) {  
  12.             System.out.println(book);  
  13.         }  
  14.         dao.getbooks();  
  15.     }  
  16.   
  17. }  

图书管理系统项目(初学者必看的小项目)
        基本框架已经实现,开始写其功能 可能会遇到很多问题加油!
[html] view plain copy
  1. <span style="font-family:'Microsoft YaHei'; font-size:14px"><strong><span style="color:#000000">目前是1.0  等今后有时间升级2.0   有兴趣的可以问我要代码  项目没完成就不在这发了  下周一在去完善 </span></strong></span>  
[html] view plain copy
  1. <span style="font-family:'Microsoft YaHei'; font-size:14px"><strong><span style="color:#000000">通宵把我之前写的东西重新捋一遍   好累 会的太少  完成2.0 就准备往下学 等以后在此基础上 升级  </span></strong></span>  
[html] view plain copy
  1. <span style="font-family:'Microsoft YaHei'; font-size:14px"><strong><span style="color:#000000">看我博客的同学 如果有兴趣的可以和我一起研究          今天通宵把其他功能全部实现,就差最后一步(搜索还有点小BUG) 加了很多图片音乐 加了个数字时钟          我打算以后做一个UI用户登录  今后学到新的知识再往上加 (本人菜鸟一只  希望大佬指点一二)</span></strong></span>  
[html] view plain copy
  1. <span style="font-family:'Microsoft YaHei'; font-size:14px"><strong><span style="color:#000000"></span></strong></span>  
[java] view plain copy
  1. <body>  
  2. <center>  
  3.  <marquee width="300" direction=left height="50"><center><font color="pink" size="7" face="华文彩云"><b>图书信息</b></font></center></marquee>  
  4. <embed src="music/3.mp3" width="0.01" height="0.01"></embed>  
  5. <table class="gridtable" id="gridtable" height="400" width="30%">  
  6.      <%    
  7.     request.setCharacterEncoding("UTF-8");  
  8.     response.setCharacterEncoding("UTF-8");  
  9.     String name=request.getParameter("name");   
  10.     //name+="%"+name+"%";  
  11.     String condition=request.getParameter("condition");   
  12.  //   out.print(name);  
  13.     BookDAO dao=new BookDAO();  
  14.     ArrayList<Book> list=dao.serachBook(name);  
  15.     if(list==null || list.size()==0){  
  16.         out.print("没用找到相应的图书");  
  17.     }  
  18.     for(int i=0;i<list.size();i++){  
  19.         Book book=list.get(i);  
  20.     %>     
  21.   
  22.     <tr>  
  23.     <td><%=i+1 %><input type="hidden" name="id" value="<%=book.getId()%>"></td>  
  24.     <td><h3><%=book.getName() %></h3></td>  
  25.     <td><h3><%=book.getAuthor() %></h3></td>  
  26.     <td><h3><%=book.getPrice() %></h3></td>  
  27.     <td><h3><%=book.getPublisher() %></h3></td>  
  28.     </tr>  
  29.     <%  
  30.         }  
  31.     %>  
  32.   
  33. </table>  
  34. </center>  
  35. </body>  
     
[html] view plain copy
  1. <body>  
  2. <embed src="music/3.mp3" width="0.01" height="0.01"></embed>  
  3. <center>  
  4.  <div class="divMain">      
  5.  <marquee width="300" direction=left height="50"><center><font color="#0000ff" size="7" face="华文彩云"><b>跟小刘学java   从入门到精通!</b></font></center></marquee>  
  6.   
  7. <h1 align="center" class="STYLE1">请输入要查询的关键字</h1>  
  8.  <form action="seek.jsp" method="post">  
  9. <h2><span class="STYLE2">  请选择查询条件</span></h2>  
  10. <select name="condition">  
  11. <option value ="author">作者</option>  
  12. <option value ="name" selected="selected">书名</option>  
  13. </select>  
  14. <span class="STYLE3">请输入</span>  
  15. <input type="text" name="name" /><br />  
  16. <input type="submit" value="提交信息 " />  
  17. </form>  
  18.   
  19. </div>  
  20. </center>  
  21. </body>  
图书管理系统项目(初学者必看的小项目)
图书管理系统项目(初学者必看的小项目)

 搜索功能完善。 图书管理就这么告一段落! 感谢任老师,范老师的细心指导答疑 
       在观看遇到问题的可以联系我 我QQ1443509763
 全部代码都放在我博客上了,可以去下载完整的项目 

相关文章:

  • 2021-10-07
  • 2022-01-21
  • 2021-11-03
  • 2022-01-11
  • 2021-04-06
  • 2022-01-05
  • 2021-10-10
猜你喜欢
  • 2021-04-08
  • 2021-06-03
  • 2022-01-08
  • 2022-12-23
  • 2021-11-27
相关资源
相似解决方案