21xrx.com
2024-09-19 09:54:31 Thursday
登录
文章检索 我的文章 写文章
Java期末项目设计文档:实现一个简单的图书管理系统
2023-06-13 20:41:47 深夜i     --     --
Java 图书管理系统 数据库连接 添加图书 删除图书

在我们日常生活中,图书管理系统得到了广泛的应用,它可以帮助我们更加高效地管理图书,为读者提供更好的服务。在本文中,我们将介绍一个基于Java的图书管理系统的设计和实现过程。

代码案例

为了实现图书管理系统,我们需要使用Java语言和MySQL数据库。下面是本系统的主要代码实现:

1. 数据库连接类


public class DBHelper {

  private static final String DRIVER = "com.mysql.jdbc.Driver";

  private static final String URL = "jdbc:mysql://localhost:3306/library";

  private static final String USERNAME = "root";

  private static final String PASSWORD = "root";

  public static Connection getConnection() throws SQLException {

    try {

      Class.forName(DRIVER);

      Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

      return connection;

    } catch (ClassNotFoundException e) {

      throw new SQLException(e);

    }

  }

}

2. 添加图书类


public class AddBook {

  private String bookName;

  private String author;

  private String publisher;

  private String price;

  public void setBookName(String bookName)

    this.bookName = bookName;

  

  public void setAuthor(String author)

    this.author = author;

  

  public void setPublisher(String publisher)

    this.publisher = publisher;

  

  public void setPrice(String price)

    this.price = price;

  

  public boolean insertBook() throws SQLException {

    Connection connection = null;

    PreparedStatement preparedStatement = null;

    try {

      connection = DBHelper.getConnection();

      String sql = "INSERT INTO book (bookName, author, publisher, price) VALUES (?, ?, ?, ?)";

      preparedStatement = connection.prepareStatement(sql);

      preparedStatement.setString(1, bookName);

      preparedStatement.setString(2, author);

      preparedStatement.setString(3, publisher);

      preparedStatement.setString(4, price);

      int rows = preparedStatement.executeUpdate();

      if (rows > 0)

        return true;

       else

        return false;

      

    } finally {

      if (preparedStatement != null) {

        preparedStatement.close();

      }

      if (connection != null) {

        connection.close();

      }

    }

  }

}

3. 删除图书类


public class DeleteBook {

  private String bookId;

  public void setBookId(String bookId)

    this.bookId = bookId;

  

  public boolean deleteBook() throws SQLException {

    Connection connection = null;

    PreparedStatement preparedStatement = null;

    try {

      connection = DBHelper.getConnection();

      String sql = "DELETE FROM book WHERE bookId=?";

      preparedStatement = connection.prepareStatement(sql);

      preparedStatement.setString(1, bookId);

      int rows = preparedStatement.executeUpdate();

      if (rows > 0)

        return true;

       else

        return false;

      

    } finally {

      if (preparedStatement != null) {

        preparedStatement.close();

      }

      if (connection != null) {

        connection.close();

      }

    }

  }

}

关键词

Java,图书管理系统,数据库连接,添加图书,删除图书

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复