First, the origin of the problem

When beginners start to do management system, they finish it in console, give the client, then input the specific operation, and the problem arises. As long as they close the eclipse data, it will disappear, which will cause great inconvenience. Then, we start to try to save information in text mode, but this operation is too cumbersome. Finally, I want to give the best solution. The solution is to use the database to complete the storage operation.

Second, case analysis

Create table

Writing java code

package com.lianxi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class Demo {
	//Given boolean value for judgment
	private boolean isTrue=true;
	//Create the start method
	public void start() throws ClassNotFoundException{
		//Given a scanner to input operation instructions
		Scanner sc=new Scanner(System.in);
		//What are the specific operations given?
		System.out.println("Please enter operation 1(Add to)2(query)3(delete)4(Amendment 5 (Exit)");
		//Do the corresponding operation according to the value input by the user
		int key=sc.nextInt();
		//Given a dead loop, the program does not exit until the user enters and exits
		while(isTrue){
			switch(key){
			case 1:add();break;
			case 2:find();break;
			case 3:delete();break;
			case 4:update();break;
			case 5:exit();break;
			default:
				System.out.println("No operation, please re-enter the operation instructions!");
				start();
				break;
			}
		}
	}
	private void update() throws ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/testaa";
		String user="root";
		String password=("123456");
		Connection cn=null;
		Statement sta=null;
		 try {
			cn = DriverManager.getConnection(url,user,password);
			String s="update department set depname='Learning Department' where depno in (2,3)";
			 sta = cn.createStatement();
			 sta.executeUpdate(s);
			 System.out.println("Successful revision!");
			 start();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				sta.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	private void delete() throws ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/testaa";
		String user="root";
		String password="123456";
		Connection cn=null;
		Statement sta=null;
		      try {
				cn = DriverManager.getConnection(url,user,password);
				  String s="delete from department where depno=5";
				   sta = cn.createStatement();
				       sta.executeUpdate(s);
				       System.out.println("Delete successful");
				       start();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					sta.close();
					cn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		
	}
	private void find() throws ClassNotFoundException {
		//Load driver
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql://localhost:3306/testaa";
		String user="root";
		String password="123456";
		Connection cn=null;
		Statement sta =null;
		ResultSet res=null;
		try {
			cn = DriverManager.getConnection(url,user,password);
			 String s="Select * from department";
			 sta = cn.createStatement();
			 res= sta.executeQuery(s);
			 //Judge whether there is value in the table, if there is output, if there is no stop.
			 while(res.next()){
				 int did=res.getInt(1);
				 String dname=res.getString(2);
				 String dmark=res.getString(3);
				 System.out.println(did+" "+dname+" "+dmark);
			 }
			 //Callback method
			 start();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				res.close();
				sta.close();
				cn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	//Add data
	private void add() throws ClassNotFoundException {
		//Load driver
		Class.forName("com.mysql.jdbc.Driver");
		//Specify Library paths
		String url="jdbc:mysql://localhost:3306/testaa";
		//Determine the username
		String user="root";
		//Confirm password
		String password="123456";
		//Initialization
		Connection cn=null;
		Statement sta=null;
		   try {
			   //Driver access connection
			cn = DriverManager.getConnection(url,user,password);
			//Given the addition of SQL statements
			String s="insert into department(depname,remark) values('Finance Department','300')";
			//Creating Connection Objects
			sta = cn.createStatement();
			//Operation of running SQL statements
			sta.executeUpdate(s);
			//Return a friendly prompt
			System.out.println("Add success!");
			//Callback method
			start();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				//Close connection
				cn.close();
				//Close the connection object
				sta.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	//Exit system
	private void exit() {
		System.out.println("Thank you for using.,See you next time!");
		System.exit(1);
	}

	public static void main(String[] args) throws ClassNotFoundException {
		//Create an object and call the start method
		Demo d=new Demo();
		d.start();
	}
}

test result


Summary

We can operate and save data well by using database, but the connecting steps of database should be remembered: 1. add database driver; 2. specify the path of database; 3. specify the login user name and password; 4. drive to get connection; 5. specify the database to be operated; 6. drive to create connection object; 7. call method of connection object to execute database instruction. 8. Close drive connections and connection objects.