Create a simple library management system and basic operations

Create a simple library management system

1. Required software and resources
①eclipse (Javaweb development environment has been configured)
②Mysql and Navicat for MySQL
③MySQL driver

2.
Create a project in eclipse Create a project named bms (Books Management System)
Create several packages in src as follows
① com.software.util // used as a framework
② com.software.test // used to test
③ com.software.dao // storage method
④ com.software.daoimpl // implement method interface
⑤ com.software.po // storage entity class

3. The specific implementation of each package
① com.software.po
creates a class book in the package which contains the book properties and construction method

package com.software.po;

public class Book {
	private int bookID;
	private String bookISBN;
	private String bookName;
	private float bookPrice;
	private String bookAuthor;
	private String bookPublisher;
	private int bookCount;
	public int getBookID() {
		return bookID;
	}
	public void setBookID(int bookID) {
		this.bookID = bookID;
	}
	public String getBookISBN() {
		return bookISBN;
	}
	public void setBookISBN(String bookISBN) {
		this.bookISBN = bookISBN;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public float getBookPrice() {
		return bookPrice;
	}
	public void setBookPrice(float bookPrice) {
		this.bookPrice = bookPrice;
	}
	public String getBookAuthor() {
		return bookAuthor;
	}
	public void setBookAuthor(String bookAuthor) {
		this.bookAuthor = bookAuthor;
	}
	public String getBookPublisher() {
		return bookPublisher;
	}
	public void setBookPublisher(String bookPublisher) {
		this.bookPublisher = bookPublisher;
	}
	public int getBookCount() {
		return bookCount;
	}
	public void setBookCount(int bookCount) {
		this.bookCount = bookCount;
	}
	public Book(int bookID, String bookISBN, String bookName, float bookPrice, String bookAuthor, String bookPublisher,
			int bookCount) {
		super();
		this.bookID = bookID;
		this.bookISBN = bookISBN;
		this.bookName = bookName;
		this.bookPrice = bookPrice;
		this.bookAuthor = bookAuthor;
		this.bookPublisher = bookPublisher;
		this.bookCount = bookCount;
	}
	public Book(String bookISBN, String bookName, float bookPrice, String bookAuthor, String bookPublisher,
			int bookCount) {
		super();
		this.bookISBN = bookISBN;
		this.bookName = bookName;
		this.bookPrice = bookPrice;
		this.bookAuthor = bookAuthor;
		this.bookPublisher = bookPublisher;
		this.bookCount = bookCount;
	}
	public Book() {
		super();
	}
	
}


In order to clarify the specific work that our library management system needs to complete, we can first use a specific class to implement this as a reference

Create a DBUtilTmp class in com.software.util

package com.software.util;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class DBUtilTmp {

	public static void main(String[] args) {
		//准备数据库的资源
		//1 用户名
		String user = "root";
		//密码
		String password = "256256";
		//url 数据库连接的地址
		//如果安装在服务器或者虚拟机上 localhost 改为ip地址
		String url ="jdbc:mysql://localhost:3306/bms?characterEncoding=utf-8";
		//驱动名 
		//8.几的版本 用String driver="com.mysql.cj.jdbc.Driver";
		String driver="com.mysql.jdbc.Driver";
		try {
			//1 加载驱动
			Class.forName(driver);
			
			//2 获取连接
			Connection conn=DriverManager.getConnection(url, user, password);
			
			//3 SQL 指令
			String sql = "select * from book";
			
			//4 创建 preparedStatement 对象 通过该对象将SQL 指令发送给MYSQL 
			//PreparedStatement 实例包含已编译的 SQL 语句.其执行速度要快于 Statement 对象
			PreparedStatement pst = conn.prepareStatement(sql);
			
			//5 执行 SQL 指令 保存结果
			ResultSet rs= pst.executeQuery();
			
			//6 循环输入结果集
			while(rs.next())
			{
				System.out.println("图书ID:"+ rs.getInt("book_id"));
				//System.out.println("图书ID:"+ rs.getInt(1));
				System.out.println("图书ISBN:"+ rs.getString("book_isbn"));
				System.out.println("图书名称:"+ rs.getString("book_name"));
				System.out.println("图书价格:"+ rs.getFloat("book_price"));
				System.out.println("图书作者:"+ rs.getString("book_author"));
				System.out.println("图书出版社:"+ rs.getString("book_publisher"));
				System.out.println("图书数量:"+ rs.getInt("book_count"));
				
			}
			//7 释放资源
			if (rs !=null)
			{
				rs.close();
			}
			if (pst !=null)
			{
				pst.close();
			}
			if (conn !=null)
			{
				conn.close();
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

In order to achieve the connection with the database, you need to put the MySQL driver in the position shown in the figure below.
Insert picture description here
Open Navicat and create a new connection. VMware
creates a new database named bms in the connection. Create a
table named book in the database. As shown in the figure
Insert picture description here
above, we use a class The process of implementing all the information in the query table is
as follows.
Insert picture description here
We can analyze what we need to complete according to this class
. 1. Prepare the resources of the database: the username and password for logging in and the address of the database connection
2. Load the driver and get a connection with the database
3 . Create a SQL instruction and execute the output
4. Finally release resources

In order to realize that the book management system can manage different books, the deterministic operations should be placed separately in the dao package. Only the general operations are kept in DBUtil

package com.software.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	private static String user="root";
	private static String password="256256";
	private static String url="jdbc:mysql://localhost:3306/bms?characterEncoding=utf-8";
	private static String driver="com.mysql.jdbc.Driver";
	private static Connection conn = null;
	private static PreparedStatement pst = null;
	private static ResultSet rs= null;
	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Connection getConnection() throws SQLException 
	{
		return DriverManager.getConnection(url, user, password);
	}
	public static void release() throws SQLException
	{
		if (rs !=null)
		{
			rs.close();
		}
		if (pst !=null)
		{
			pst.close();
		}
		if (conn !=null)
		{
			conn.close();
		}
	}

}

③com.software.dao
dao package is used to store the operation of the book management system
Add book information
Delete book information
Modify book information
Find book information
Create a new interface named IBookDao in the dao package as follows

package com.software.dao;

import java.util.List;

import com.software.po.Book;

public interface IBookDao {
	
	//添加图书信息
	/*
	 * 创建方法的考虑
	 * 1.访问修饰符: public
	 * 2.返回值类型:暂定void
	 * 3.方法名  :addBook
	 * 4.参数 :Book book
	 */
	public void addBook(Book book);
	//删除图书信息
	/*
	 * 创建方法的考虑
	 * 1.访问修饰符: public
	 * 2.返回值类型: 暂定 void 
	 * 3.方法名: delBook
	 * 4.参数:String bookINSB
	 */
	public void delBook(String bookINSB);
	//更改图书信息
	/*
	 * 创建方法的考虑
	 * 1.访问修饰符: public
	 * 2.返回值类型: 暂定 void
	 * 3.方法名: updataBook
	 * 4.参数: Book book
	 */
	public void updataBook(Book book);
	//查询图书信息
	/*
	 * 创建方法的考虑
	 * 1.访问修饰符: public
	 * 2.返回值类型: List<Book>
	 * 3.方法名: findAllBooks
	 * 4.参数: 无
	 */
	public List<Book> findAllBooks();
}


com.software.daoimpl creates a new class named BookDaoimpl in daoimpl.
This class is a specific implementation of the methods in the dao package.
Here is a lookup as an example.

package com.soft.ware.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.software.dao.IBookDao;
import com.software.po.Book;
import com.software.util.DBUtil;

public class BookDaoimpl implements IBookDao {

	@Override
	public void addBook(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delBook(String bookINSB) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updataBook(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Book> findAllBooks() {
		List<Book> books = new ArrayList<Book>();
		try {
			Connection conn = DBUtil.getConnection();
			String sql = "select * from book";
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet rs= pst.executeQuery();
			while(rs.next())
			{
				Book book= new Book();
				book.setBookID(rs.getInt("book_id"));
				book.setBookISBN(rs.getString("book_isbn"));
				book.setBookName(rs.getString("book_name"));
				book.setBookPrice(rs.getFloat("book_price"));
				book.setBookAuthor(rs.getString("book_author"));
				book.setBookPublisher(rs.getString("book_publisher"));
				book.setBookCount(rs.getInt("book_count"));
				books.add(book);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				DBUtil.release();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return books;
	}

}

⑤com.software.test and
finally create a new class named FindAllBooksTest in the test package for testing

package com.software.test;

import java.util.List;

import com.soft.ware.dao.impl.BookDaoimpl;
import com.software.dao.IBookDao;
import com.software.po.Book;

public class FindAllBooksTest {
	public static void main(String[] args) {
		IBookDao bookDao = new BookDaoimpl();
		List<Book> books = bookDao.findAllBooks();
		for(Book book:books)
		{
			System.out.println("图书ID:"+ book.getBookID());
			System.out.println("图书ISBN:"+ book.getBookISBN());
			System.out.println("图书名称:"+ book.getBookName());
			System.out.println("图书价格:"+ book.getBookPrice());
			System.out.println("图书作者:"+ book.getBookAuthor());
			System.out.println("图书出版社:"+book.getBookPublisher());
			System.out.println("图书数量:"+ book.getBookCount());
		}
	}
}

The effect is as follows.
Insert picture description here
This is a summary of my experimental class. I leave it to myself. If there are errors, I hope I can correct the learning.

Published 31 original articles · won praise 8 · views 2155

Guess you like

Origin blog.csdn.net/weixin_44034024/article/details/105471309