Java Training-Library Management System

/*

  • Library Information Management System
  • Function Display: Loan, Return, Search Books According to Title, Books Addition, Books Update, Books Destroy, Exit System
  • Attributes of each book: book number, title, lending status, lending time, lending times
    */
    Key: Connecting to the database

1. Basic Training

In this training, while making progress and improvement, we encountered various problems and difficulties, especially the further understanding of Java language methods, classes, object-oriented features, and a more complete knowledge structure of Java program knowledge. We had previously not grasped the solid and ungrasped areas in learning. In practice, if you don't understand, ask if you don't understand. If you can't find the reason for the program error, ask if you don't shame. You have done a good job of checking and filling in the gaps, and constantly solve all kinds of problems and improve yourself.

1.1. Training Contents
Follow the teacher's teaching arrangement, learn step by step according to the teacher's steps, do not lag behind, do not default, do a good job in every step of learning. Talk about the basic knowledge of Java, understand the database, learn how to connect to the database by eclipse, learn how to develop programs and develop a small project independently. Strengthen the understanding and use of Java language, skillfully write the code of basic questions, learn to use Java language to design and develop programs.

1.2. Problem Arrangement and Solution
Questions:
1. How to use the database: Open the database, create a new database, create new tables, and create new queries.
2. Insert, delete, update, select the database.
3. Elipse connects to database.
4. When eclipse is input, the Int type becomes String type, and there will be program jump, which requires conversion (scan.nextLine(); conversion).

Solve:
1. Consult the teacher.
2. Ask your classmates.
3. Online inquiry.
4. Look through books.

2 Project Development-Design and Implementation of Library Information Management System

2.1 Overall Functional Description
The main functions of the library information management system are 1, lending, 2, returning, 3, inquiring books according to their titles, 4, adding books, 5, updating books, 6, destroying books, 7, quitting the system. The system function module diagram is shown in Fig. 2-1.

As shown in Figure 2-1

2.2 System Detailed Design

2.2.1 Display of Running Results


2.2.2 System Execution Flow
The system starts with the main function of program entry. First, it displays the main menu, lists all books information and function options. After entering the menu option, it executes the corresponding borrowing sub-function. After entering the menu option, it executes the corresponding borrowing sub-function. This sub-function includes querying the information of books according to the title of books, obtaining the state value, and so on. Get the time value of the sub-function; Enter 2 menu options, execute the corresponding return sub-function, this sub-function also contains query according to the title of the book information, get the state value, get the time value of the sub-function; Enter 3 menu options, execute the corresponding query according to the title of the book sub-function; Enter 4 menu options, execute the pair. The corresponding books add sub-functions; after entering 5 menu options, the corresponding book update sub-functions are executed; after entering 6 menu options, the corresponding book destruction sub-functions are executed; after entering 7 menu options, the corresponding exit system sub-functions are executed; and the program is finished. The flow chart of the system is shown in Fig. 2-2.

Figure 2-2 System Execution Flow Chart

2.3 System Function and Implementation
The program of this system is mainly used to store book information in the database, connect the database, add data information of each object to the database, find information we need by adding, deleting, modifying and searching four sql statements, and judge the function to be executed by conditional statement.

2.3.1 Database Processing and Implementation of Function and Call Database

package com.zhiyou100;
//Build an aircraft class
/*
 * 1,Property privatization in classes
 * 2,Generating get and set methods
 * 3,Generating toString method
 * 
 */
public class Plane {
	/*1, Four fields - > (called in the class) attributes - > privatization
	-->Protecting Attributes in Classes - > Private - > Private Attributes in Classes
	Enablation can only be used in the current class.
	In the program, class and class association is very high.
	Attributes in the current class need to be opened to the outside world through set and get methods*/
	private int id;//number
	private String number;//Flight number
	private String address;//Destination
	private String date;//date
	/*2,Quick Generation of get and set Method Shortcuts - > Alt + shif + s and Find
     Generate Getters and Setters Single click,
	 Then four ticks*/
	public int getId() {
		return id;
	}
	
   	public void setId(int id) {
		this.id = id;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}
	/*3,To generate toString methods, when printing attribute values in an object, you must
	toString   Keyboard shortcut - > Alt + shif + s and then find
     Generate toString ,Then the first tick*/
	@Override
	public String toString() {
		return "Plane [id=" + id + ", number=" + number + ", address=" + address + ", date=" + date + "]";
	}
}

package com.zhiyou100;
import java.util.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

//Program Entry
public class TestBook {
	static int i;
	static java.sql.Connection con = null;//Connect
	static PreparedStatement ps = null;//sql template
	static ResultSet rs = null;//Result Set
	public static void main(String[] args) throws Exception {
		System.out.println("********************Welcome to use the library information management system********************");	
		listMessage();
		show();
		Scanner scan = new Scanner(System.in);
		while(true){
			//System prompt number
			int inputNum = scan.nextInt();
			scan.nextLine();
			switch(inputNum){
			case 1:
				System.out.println("Please enter the title of the book:");
				String name01 = scan.nextLine();
				borrwBook(name01);
				break;
			case 2:
				System.out.println("Please enter the title of the book:");
				String name02 = scan.nextLine();
				backBook(name02);
				break;
			case 3:
				System.out.println("Please enter the title of the book:");
				String name03 = scan.nextLine();
				selectBook(name03, true);//Case 3 Query Books According to Title
				break;
			case 4:
				System.out.println("Add Book Number");
				String num = scan.nextLine();
				System.out.println("Add the title of the book:");
				String name04 = scan.nextLine();
				System.out.println("State of Adding Books:");
				int state = scan.nextInt();
				//The reason for program jumping is that with the Scanner class, an exception occurs when you first enter a value and then a string.
				//Conversion from number to string in console
				scan.nextLine();
				System.out.println("Add book lending time:");
				String date = scan.nextLine();
				System.out.println("The number of times books were lent:");
				int time = scan.nextInt();
				insertBook(num, name04, state,date,time);//4. Case 4 Books Addition
				break;
			case 5:
				System.out.println("Update the status of books:");
				int state03 = scan.nextInt();
				System.out.println("Update the number of books loaned:");
				int time03 = scan.nextInt();
				  scan.nextLine();
				System.out.println("Update the name of the book:");
				String name05 = scan.nextLine();
				updateBook(state03, time03, name05);
				System.out.println("Update Successful!");//Case 5 Update Books
				break;
			case 6:
				System.out.println("Destruction of titles of books:");
				String name06 = scan.nextLine();
				deleteBook(name06);//6. Case 6 Destroying Books
				break;
			default:
				scan.close();
				exit();	
				break;
			}
			show();
		}
	}
	//Register Driver, Get Connections
	public static java.sql.Connection getCon() throws Exception{
		//1. Registration Driver
		Class.forName("com.mysql.jdbc.Driver");
		//2. Getting Connections
		con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school","root","");
		return  con;
	}

	//Display the Main Menu of Library Management Information System
	public static void show(){
		System.out.println("Please select the operation:(1.Borrowing books, 2.Return the book, 3.Search books according to their titles, 4.Books Addition, 5.Books Update, 6.Destruction of Books, 7.Exit System):");
	}
	//List all book information
	public static void listMessage() throws Exception{
		//1, 2 Register Driver, Get Connections
		getCon();
		//3. Preparing sql statements
		String sql = "select * from book";
		//4. Preparing sql execution template
		ps = con.prepareStatement(sql);
		//5. Execute sql statement to get query result
		rs = ps.executeQuery();
		//6. Display data iteration
		System.out.println("Serial number"+"\t"+"number"+"\t"+"Title"+"\t"+"state"+"\t"+"Borrowing time"+"\t"+"Number of books borrowed");
		int index= 1;
		while(rs.next()){
			String name = rs.getString("name");
			String num = rs.getString("num");
			String state= rs.getString("state");
			String date = rs.getString("date");
			int time = rs.getInt("time");
			if(date == null){
				System.out.println(index+"\t"+num+"\t"+name+"\t"+state+"\t"+date+"\t        "+time);
			}
			else{
				System.out.println(index+"\t"+num+"\t"+name+"\t"+state+"\t"+date+"\t"+time);	
			}
			index++;
		}
		rs.close();
		ps.close();
		con.close();
		}
	//Query Books and Return state Value
	public static int stateBook(String name) throws Exception{
		//1, 2, Registration Driver, Access Connection
		getCon();
		//3. Preparing sql statements
		String sql = "select * from book where name = ?";
		//4. Preparing sql execution template
		ps = con.prepareStatement(sql);
		//5. Execute sql statement to get query result
		ps.setString(1, name);
		rs = ps.executeQuery();
		int state = 1;
		while(rs.next()){
			state=rs.getInt("state");
		}
		return state;
	}
	//Return the number of times borrowed
	public static int timeBook(String name) throws Exception{
		//1, 2, Registration Driver, Access Connection
		getCon();
		//3. Preparing sql statements
		String sql = "select * from book where name = ?";
		//4. Preparing sql execution template
		ps = con.prepareStatement(sql);
		//5. Execute sql statement to get query result
		ps.setString(1, name);
		rs = ps.executeQuery();
		rs.next();
		int time = rs.getInt("time");
		return time;
	}
	//state 0 is not loaned, loaned at 1:00
	//1. Borrowing Books
	public static void borrwBook(String name) throws Exception{
		//1.1 Query from the library according to the existence of books?
		boolean isBor = selectBook(name, false);
		//true does, false does not.
		if(isBor){
			int time;
			int state = stateBook(name);
			if(state == 0){
				System.out.println("Can borrow, congratulations on the success of borrowing books!");
				state = 1;
				time = timeBook(name);
				time++;
				i=time;
				updateBook(state,i,name);
			}
			else{
				System.out.println("Borrowed");
			}
		}
		else{
			System.out.println("This book is not available in the library!");
		}
	}
	//2. Return
	public static void backBook(String name) throws Exception{
		
		//true does, false does not.
		if(selectBook(name, false)){
			int state;
			state = stateBook(name);
			if(state == 0){
				System.out.println("This book has not been lent, and it need not be returned.!");
			}
			else if(state == 1){	
				System.out.println("Return success!");
				state--;
				updateBook(state,i,name);
			}
		}
	}
	//3. Search books according to their titles   
   //printFlag (1) Outputs database information (2) Statistics database information (3) Prints information if flag is true; If flag is false, only counts.
	public static boolean selectBook(String name, boolean printFlag) throws Exception{
		//1, 2, Registration Driver, Access Connection
		getCon();
		//3. Preparing sql statements
		String sql = "select * from book where name = ?";
		//4. Preparing sql execution template
		ps = con.prepareStatement(sql);
		//5. Execute sql statement to get query result
		ps.setString(1, name);
		rs = ps.executeQuery();
		//6. Print the header and output iteratively
		if (printFlag) System.out.println("Serial number"+"\t"+"number"+"\t"+"Title"+"\t"+"state"+"\t"+"Borrowing time"+"\t"+"Number of books borrowed");
		int index = 0;//Serial number
		while(rs.next()){
			if (printFlag) {
				//Get field values in turn
				int num = rs.getInt("num");
				int state = rs.getInt("state");
				String date = rs.getString("date");
				String time = rs.getString("time");
				System.out.println(index+"\t"+num+"\t"+name+"\t"+state+"\t"+date+"\t"+time);
			}
			index++;
		}
		if (index == 0) {
			//System.out.println("The library doesn't have this book! "";
			ps.close();
			con.close();
			return false;//Query End
		}
		rs.close();
		ps.close();
		con.close();
		return true;
	}
	//4. Books Addition
	public static void insertBook(String num,String name,int state,String date,int time) throws Exception{
		//1, 2, Registration Driver, Access Connection
		getCon();
		//3. Preparing sql statements
		String sql = "insert into book values(null,?,?,?,?,?)";
		//4. Preparing sql templates
		ps = con.prepareStatement(sql);
		//5. Replace? Execute sql template
		ps.setString(1,num);
		ps.setString(2,name);
		ps.setInt(3,state);
		ps.setString(4, date);
		ps.setInt(5,time);
		ps.executeUpdate();	
		//6. Close the connection
		ps.close();
		con.close();
		System.out.println("Add Complete!");
	}
	//5. The State and Number of Books Renewal and Loan
	public static void updateBook(int state,int time,String name) throws Exception{
		//1, 2, Registration Driver, Access Connection
		getCon();
		//3. Preparing sql statements
		String sql = "update book set state = ?,time = ? where name = ?";
		//4. Preparing sql execution template
		ps = con.prepareStatement(sql);
		//5. Replace? Execute sql statement to get query result
		ps.setInt(1, state);
		ps.setInt(2, time);
		ps.setString(3, name);
		ps.executeUpdate();	
		//6. Close the connection
		ps.close();
		con.close();	
	}
	
	//6. Destruction of Books According to Title and Number of Books
	public static void deleteBook(String name) throws Exception{
		//1, 2. Registration Driver Access Links
		getCon();
		//3. Preparing sql statements
		String sql = "delete from book where name = ?";
		//4. Preparing sql execution template
		ps = con.prepareStatement(sql);
		//5. Executing sql statements
		ps.setString(1, name);
		ps.executeUpdate();
		//6. Close the connection
		ps.close();
		con.close();
		System.out.println("Delete successfully!");
		
	}
	//7. Exit System
	public static void exit(){
		System.out.println("Looking forward to your next visit!");
		System.exit(0);
	}
	
}

3 Summary of Practice Training
This is a practical training, although only a short week, but the total mention of great gains, realized that many of their own shortcomings, knowledge is not sophisticated, master is not proficient. I have a clearer understanding of the basic knowledge of Java than I did before: (1) Learn to use the database and know how to add, delete and modify the query statements of the database, (add: insert into table name values(null,??,??????????); delete: delete from table name where field name = field value; change: update table name set: Modified Field Name = Modified Field Value Where Modified Data; Check: select * from Table Name Where Field Name = Field Value; Check Multiple Contents with "and" Connection, or "; Aggregate Function: Number of Checks with"count();"Average"avg()"and so on. (2) Learn how to connect a database to eclipse. There are six steps: (1) Register driver (2) Get connection (connection with Myssql database); (3) Prepare sql statement (4) Prepare sql execution template (5) Execute sql statement to get query result; (6) Close database connection. (3) Learn how to add jar. packages. Eclipse has a more detailed understanding of the various functions, more convenient programming. (4) A small development project can be completed independently. (5) printFlag function. Output database information or statistical database information, if flag is true, print information; if flag is false statistics. (6) Learned traversal: while (rs. next (){ (7) Learning Random ran = new Random(System.in);double a=ran.nextDouble();s random 0 to 1 decimal; int b = ran.nextInt;(10); random 0 to 10 integers left open and closed. (8) Learn some new tips, such as: System.out.println(); abbreviated as "syso"; in eclipse, words with red fonts are keywords. Through this training, I not only consolidated and deepened the theoretical knowledge of Java programming, but also gained valuable experience through mobile phone opportunities for project development.

Tags: SQL Database Java Eclipse

Posted on Tue, 03 Sep 2019 04:38:18 -0700 by Ruiser