图书管理系统

  • M3_624531
    了解作者
  • 24MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-20 12:52
    上传日期
这个项目包含图书信息和照片的增删改查,通过使用struts2框架
book.zip
内容介绍
package dao; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class BookDao { private static InitialContext context=null; private DataSource dataSource=null; public BookDao(){ try { if(context==null){ context=new InitialContext(); } dataSource= (DataSource) context.lookup("java:comp/env/jdbc/mysql"); } catch (NamingException e) { e.printStackTrace(); } } //添加图书时判断是否已存在此图书编号 public boolean bookfindone(String bookid){ Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; try { connection=dataSource.getConnection(); String sql="select * from book where bookid=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,bookid); resultSet=preparedStatement.executeQuery(); if(resultSet.next()){ //System.out.println("333"); return true; } }catch (SQLException e){ e.printStackTrace(); } //System.out.println("444"); return false; } //根据编号查找到该图书 public Book bookfindonebyid(String bookid){ Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; Book book=new Book(); try { connection=dataSource.getConnection(); //System.out.println("105"); String sql="select * from book where bookid=?"; //System.out.println("106"); preparedStatement=connection.prepareStatement(sql); //System.out.println("107"); preparedStatement.setString(1,bookid); //System.out.println("108"); resultSet=preparedStatement.executeQuery(); //System.out.println("109"); if (resultSet.next()){ // System.out.println("110"); book.setBookid(resultSet.getString(1)); book.setBookname(resultSet.getString(2)); book.setBookprice(resultSet.getDouble(3)); //将路径中的文件名提取出来 String bookpath1=resultSet.getString(4); File tempFile=new File(bookpath1.trim()); String bookpath=tempFile.getName(); book.setBookpath(bookpath); } }catch (SQLException e){ //System.out.println("111"); e.printStackTrace(); } return book; } //添加图书信息 public boolean bookadd(Book book){ Connection connection=null; PreparedStatement preparedStatement=null; try { connection = dataSource.getConnection(); String sql="insert into book values(?,?,?,?)"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,book.getBookid()); preparedStatement.setString(2,book.getBookname()); preparedStatement.setDouble(3,book.getBookprice()); preparedStatement.setString(4,book.getBookpath()); //System.out.println("111"); preparedStatement.executeUpdate(); return true; }catch (SQLException e) { e.printStackTrace(); } return false; } public List<Book> bookshow(){ List<Book> list=new ArrayList<>(); Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; try { connection = dataSource.getConnection(); String sql="select * from book"; preparedStatement=connection.prepareStatement(sql); resultSet=preparedStatement.executeQuery(); while(resultSet.next()){ Book book=new Book(); book.setBookid(resultSet.getString("bookid")); //System.out.println(resultSet.getString("bookid")); book.setBookname(resultSet.getString("bookname")); book.setBookprice(resultSet.getDouble("bookprice")); //将路径中的文件名提取出来 String bookpath1=resultSet.getString("bookpath"); File tempFile=new File(bookpath1.trim()); String bookpath=tempFile.getName(); book.setBookpath(bookpath); list.add(book); //System.out.println(list); } //System.out.println("222"); return list; }catch (SQLException e){ e.printStackTrace(); } //System.out.println("333"); return null; } //修改图片 public boolean bookchange1(Book book){ Connection connection=null; PreparedStatement preparedStatement=null; try { connection=dataSource.getConnection(); String sql="update book set bookname=?,bookprice=?,bookpath=? where bookid=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,book.getBookname()); preparedStatement.setDouble(2,book.getBookprice()); preparedStatement.setString(3,book.getBookpath()); preparedStatement.setString(4,book.getBookid()); preparedStatement.executeUpdate(); return true; }catch (SQLException e){ e.printStackTrace(); } return false; } public boolean bookchange2(Book book){ Connection connection=null; PreparedStatement preparedStatement=null; try { connection=dataSource.getConnection(); String sql="update book set bookname=?,bookprice=? where bookid=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,book.getBookname()); preparedStatement.setDouble(2,book.getBookprice()); preparedStatement.setString(3,book.getBookid()); preparedStatement.executeUpdate(); return true; }catch (SQLException e){ e.printStackTrace(); } return false; } public boolean bookdelete(String bookid){ Connection connection=null; PreparedStatement preparedStatement=null; try{ connection=dataSource.getConnection(); String sql="delete from book where bookid=?"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1,bookid); preparedStatement.executeUpdate(); return true; }catch (SQLException e){ e.printStackTrace(); } return false; } //分页 public List<Book> bookshow1(int currentPage,int pageSize){ List<Book> book=new ArrayList<>(); book=this.bookshow(); int listLength=0; listLength=book.size(); int startIndex=(currentPage-1)*pageSize;//startIndex每一页开始的第一个编号 int endIndex=startIndex+pageSize; if(endIndex>listLength){ endIndex=listLength; } book=book.subList(startIndex,endIndex); return book; } }
评论
    相关推荐
    • struts
      本书以简单易懂的实例,介绍了Struts的知识。
    • Struts
      Struts简易教程
    • Struts
      北大青鸟Struts 上课的示例
    • struts
      struts 學習資料.
    • struts
      Apache Struts项目提供了Apache Struts 2 Web框架,该框架是用于创建基于Web的Java应用程序的全面的模块化工具堆栈。 来自WebWork 2框架的Struts 2,对于重视解决难题的优雅解决方案的团队来说,是一个绝佳的选择。...
    • struts
      struts源代码与例子
    • struts
      struts中应用程序的安全性, struts中大数据集的分页处理, struts中处理大记录数分页, struts文件上传与下载, struts输入验证和数据据转换, strutstaglib进阶.ppt
    • struts
      Apache Struts项目提供了Apache Struts 2 Web框架,该框架是用于创建基于Web的Java应用程序的全面的模块化工具堆栈。 来自WebWork 2框架的Struts 2,对于重视解决难题的优雅解决方案的团队来说,是一个绝佳的选择。...
    • struts
      Apache Struts项目提供了Apache Struts 2 Web框架,该框架是用于创建基于Web的Java应用程序的全面的模块化工具堆栈。 来自WebWork 2框架的Struts 2,对于重视解决难题的优雅解决方案的团队来说,是一个绝佳的选择。...
    • StrutsStruts
      达内Struts课件.rar 达内Struts课件.rar 达内Struts课件.rar