欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)簡易學生管理系統(tǒng)

 更新時間:2022年07月25日 14:45:39   作者:沉河不浮  
這篇文章主要為大家詳細介紹了Java實現(xiàn)簡易學生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java實現(xiàn)學生管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

1.學生管理系統(tǒng)(控制臺界面實現(xiàn))

//學生類,繼承Serializeable接口,將其序列化寫入文件
class Student implements Comparable<Student>,Serializable
{
? ? private int id;
? ? private String name;
? ? private int age;
? ? public Student(){
? ? ? ? id=0;
? ? ? ? name=null;
? ? ? ? age=0;
? ? }
? ? public Student(int id,String name,int age){
? ? ? ? this.id=id;
? ? ? ? this.name=name;
? ? ? ? this.age=age;
? ? }
? ? public int getId(){
? ? ? ? return id;
? ? }
? ? public void setId(int id){
? ? ? ? this.id=id;
? ? }
? ? public String getName(){
? ? ? ? return name;
? ? }
? ? public void setName(String name){
? ? ? ? this.name=name;
? ? }
? ? public int getAge(){
? ? ? ? return age;
? ? }
? ? public void setAge(int age){
? ? ? ? this.age=age;
? ? }
? ? @Override
? ? public String toString(){
? ? ? ? return "id:"+id+" name:"+name+" age:"+age;
? ? }
? ? //重載equals和compareTo函數(shù),分別后續(xù)比較
? ? @Override
? ? public boolean equals(Object obj){
? ? ? ? if(this==obj)
? ? ? ? ? ? return true;
? ? ? ? if(getClass()!=obj.getClass()){
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? Student other=(Student)obj;
? ? ? ? if(other.name==null){
? ? ? ? ? ? if(this.name!=null)
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? else if(this.id==other.id&&this.age==other.age)
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? else
? ? ? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? if(this.name.equals(other.name)){
? ? ? ? ? ? if((this.age==other.age)&&(this.id==other.id))
? ? ? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return false;
? ? }
? ? @Override
? ? public int compareTo(Student other){
? ? ? ? if(this.id>other.id)
? ? ? ? ? ? return 1;
? ? ? ? else if(this.id<other.id)
? ? ? ? ? ? return -1;
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? if(this.age>other.age)
? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? else if(this.age<other.age)
? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? else
? ? ? ? ? ? ? ? return this.name.compareTo(other.name);
? ? ? ? }
? ? }
}
class Manage
{
? ? private ArrayList<Student>arrayList=new ArrayList<>();
? ? private final String filename="student.dat";
? ? public Manage(){
? ? ? ? try{
? ? ? ? ? ? FileInputStream file=new FileInputStream(new File(filename));
? ? ? ? ? ? ObjectInputStream inputStream=new ObjectInputStream(file);
? ? ? ? ? ? while(true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Student stu=(Student)inputStream.readObject();
? ? ? ? ? ? ? ? if(stu==null)
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? arrayList.add(stu);
? ? ? ? ? ? }
? ? ? ? ? ? //先關(guān)閉對象流,再關(guān)閉文件流
? ? ? ? ? ? inputStream.close();
? ? ? ? ? ? file.close();
? ? ? ? }
? ? ? ? catch(Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public void Menu(){
? ? ? ? boolean flag=true;
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? while(flag)
? ? ? ? {
? ? ? ? ? ? System.out.println("-----------------------------------");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? System.out.println("|-------------1.添加學生-----------|");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? System.out.println("|-------------2.修改學生-----------|");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? System.out.println("|-------------3.刪除學生-----------|");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? System.out.println("|-------------4.查找學生-----------|");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? System.out.println("|-------------5.顯示學生-----------|");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? System.out.println("|-------------6.退出系統(tǒng)-----------|");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |");
? ? ? ? ? ? System.out.println("-----------------------------------");
? ? ? ? ? ? System.out.print("請輸入您的選擇:");
? ? ? ? ? ? int choice=input.nextInt();
? ? ? ? ? ? switch(choice){
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? this.AddStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? this.ModifyStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? this.DeleteStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? this.FindStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? ? ? this.ShowStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? ? ? FileOutputStream outputStream=new FileOutputStream(filename);
? ? ? ? ? ? ? ? ? ? ? ? ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
? ? ? ? ? ? ? ? ? ? ? ? for(Student stu:arrayList){
? ? ? ? ? ? ? ? ? ? ? ? ? ? objectOutputStream.writeObject(stu);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? objectOutputStream.close();
? ? ? ? ? ? ? ? ? ? ? ? outputStream.close();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? catch(Exception e){
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? System.out.println("歡迎下次使用");
? ? }
? ? public void AddStudent(){
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? try{
? ? ? ? ? ? FileOutputStream outputStream=new FileOutputStream(filename,true);
? ? ? ? ? ? ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
? ? ? ? ? ? String name;
? ? ? ? ? ? int id,age;
? ? ? ? ? ? boolean flag=true;
? ? ? ? ? ? while(flag){
? ? ? ? ? ? ? ? System.out.print("請輸入學號:");
? ? ? ? ? ? ? ? id=input.nextInt();
? ? ? ? ? ? ? ? //重新new Scanner,防止將換行符賦給name
? ? ? ? ? ? ? ? input=new Scanner(System.in);
? ? ? ? ? ? ? ? System.out.print("請輸入姓名:");
? ? ? ? ? ? ? ? name=input.nextLine();
? ? ? ? ? ? ? ? System.out.print("請輸入年齡:");
? ? ? ? ? ? ? ? age=input.nextInt();
? ? ? ? ? ? ? ? Student stu=new Student(id,name,age);
? ? ? ? ? ? ? ? objectOutputStream.writeObject(stu);
? ? ? ? ? ? ? ? arrayList.add(stu);
? ? ? ? ? ? ? ? System.out.println("是否繼續(xù)添加?");
? ? ? ? ? ? ? ? System.out.println("1.Yes/2.No");
? ? ? ? ? ? ? ? int choice=input.nextInt();
? ? ? ? ? ? ? ? if(choice==2)
? ? ? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? }
? ? ? ? ? ? objectOutputStream.close();
? ? ? ? ? ? outputStream.close();
? ? ? ? ? ? System.out.println("添加成功");
? ? ? ? }
? ? ? ? catch(Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? //等待1秒,方便實驗者觀察結(jié)果
? ? ? ? try {
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch(InterruptedException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public void ModifyStudent(){
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? System.out.print("請輸入要修改的學生姓名:");
? ? ? ? String name=input.nextLine();
? ? ? ? boolean flag=false;
? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? {
? ? ? ? ? ? if(name.equals(arrayList.get(i).getName()))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? flag=true;
? ? ? ? ? ? ? ? System.out.println(i+" "+arrayList.get(i).toString());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if(!flag)
? ? ? ? {
? ? ? ? ? ? System.out.println("修改失敗");
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? System.out.print("請輸入要修改的學生對應的序號:");
? ? ? ? ? ? input=new Scanner(System.in);
? ? ? ? ? ? int index=input.nextInt();
? ? ? ? ? ? System.out.print("請輸入學生學號:");
? ? ? ? ? ? int id=input.nextInt();
? ? ? ? ? ? System.out.print("請輸入學生姓名:");
? ? ? ? ? ? input=new Scanner(System.in);
? ? ? ? ? ? name=input.nextLine();
? ? ? ? ? ? System.out.print("請輸入學生年齡:");
? ? ? ? ? ? int age=input.nextInt();
? ? ? ? ? ? arrayList.set(index,new Student(id,name,age));
? ? ? ? ? ? try{
? ? ? ? ? ? ? ? FileOutputStream outputStream=new FileOutputStream(filename);
? ? ? ? ? ? ? ? ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
? ? ? ? ? ? ? ? for(Student stu:arrayList){
? ? ? ? ? ? ? ? ? ? objectOutputStream.writeObject(stu);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? objectOutputStream.close();
? ? ? ? ? ? ? ? outputStream.close();
? ? ? ? ? ? }
? ? ? ? ? ? catch(Exception e){
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println("修改成功");
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch(InterruptedException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public void DeleteStudent(){
? ? ? ? Map<Integer,Student>map=new HashMap<>();
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? System.out.print("請輸入學生姓名:");
? ? ? ? String name=input.nextLine();
? ? ? ? boolean flag=false;
? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? {
? ? ? ? ? ? if(name.equals(arrayList.get(i).getName()))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? flag=true;
? ? ? ? ? ? ? ? System.out.println(i+":"+arrayList.get(i).toString());
? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if(!flag)
? ? ? ? {
? ? ? ? ? ? System.out.println("刪除失敗");
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? System.out.print("請輸入要刪除的學生序號:");
? ? ? ? ? ? int id=input.nextInt();
? ? ? ? ? ? if(map.containsKey(id)){
? ? ? ? ? ? ? ? arrayList.remove(map.get(id));
? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? FileOutputStream outputStream=new FileOutputStream(filename);
? ? ? ? ? ? ? ? ? ? ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
? ? ? ? ? ? ? ? ? ? for(Student stu:arrayList){
? ? ? ? ? ? ? ? ? ? ? ? objectOutputStream.writeObject(stu);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? objectOutputStream.close();
? ? ? ? ? ? ? ? ? ? outputStream.close();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch(Exception e){
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? System.out.println("刪除失敗");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch(InterruptedException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public void FindStudent(){
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? System.out.println("請輸入學生姓名:");
? ? ? ? String stuname=input.nextLine();
? ? ? ? if(arrayList.size()==0)
? ? ? ? {
? ? ? ? ? ? System.out.println("當前系統(tǒng)無學生信息");
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? boolean flag=false;
? ? ? ? ? ? for(Student stu:arrayList){
? ? ? ? ? ? ? ? if(stuname.equals(stu.getName()))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? flag=true;
? ? ? ? ? ? ? ? ? ? System.out.println(stu.toString());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if(!flag)
? ? ? ? ? ? ? ? System.out.println("查無此人");
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch(InterruptedException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? public void ShowStudent(){
? ? ? ? if(arrayList.size()==0)
? ? ? ? {
? ? ? ? ? ? System.out.println("當前系統(tǒng)中無學生信息");
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? int num=0;
? ? ? ? for(Student stu:arrayList){
? ? ? ? ? ? System.out.println((num++)+":"+stu.toString());
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch(InterruptedException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}
public class test{
? ? public static void main(String[]args){
? ? ? ? Manage manage=new Manage();
? ? ? ? manage.Menu();
? ? }
}

學生管理系統(tǒng)效果圖

2.學生管理系統(tǒng)(圖形化界面實現(xiàn))

使用Java提供的Javax庫來實現(xiàn)圖形化界面,在使用這個庫的時候,我發(fā)現(xiàn)它和Qt十分相似,但和Qt相比感覺更方便使用。

package project.demo;
import java.io.*;
import java.util.ArrayList;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//因為要寫入對象文件,所以必須序列化
class Students implements ?Serializable{
? ? private int id;
? ? private String name;
? ? private int age;
? ? public Students(int id,String name,int age){
? ? ? ? this.id=id;
? ? ? ? this.name=name;
? ? ? ? this.age=age;
? ? }
? ? public int getId(){
? ? ? ? return id;
? ? }
? ? public String getName(){
? ? ? ? return name;
? ? }
? ? public int getAge(){
? ? ? ? return age;
? ? }
? ? public String toString(){
? ? ? ? return id+" "+name+" "+age;
? ? }
? ? public void setId(int id){
? ? ? ? this.id=id;
? ? }
? ? public void setName(String name){
? ? ? ? this.name=name;
? ? }
? ? public void setAge(int age){
? ? ? ? this.age=age;
? ? }
}
class ManageSystem{
? ? private final static String filename="students.dat";
? ? private ArrayList<Students>arrayList=new ArrayList<>();
? ? //主界面
? ? JFrame jFrame=new JFrame("Student Manage System");
? ? //按鈕
? ? JButton addButton=new JButton("添加學生");
? ? JButton modifyButton=new JButton("修改學生");
? ? JButton deleteButton=new JButton("刪除學生");
? ? JButton searchButton=new JButton("查找學生");
? ? JButton showButton=new JButton("顯示學生");
? ? JButton exitButton=new JButton("退出系統(tǒng)");
? ? public ManageSystem(){
? ? ? ? //設(shè)置界面的大小,位置,以及組件
? ? ? ? jFrame.setSize(800,700);
? ? ? ? jFrame.setLocation(600,200);
? ? ? ? jFrame.setLayout(null);
? ? ? ? addButton.setBounds(200,50,400,75);
? ? ? ? modifyButton.setBounds(200,150,400,75);
? ? ? ? deleteButton.setBounds(200,250,400,75);
? ? ? ? searchButton.setBounds(200,350,400,75);
? ? ? ? showButton.setBounds(200,450,400,75);
? ? ? ? exitButton.setBounds(200,550,400,75);
? ? ? ? jFrame.add(addButton);
? ? ? ? jFrame.add(modifyButton);
? ? ? ? jFrame.add(deleteButton);
? ? ? ? jFrame.add(searchButton);
? ? ? ? jFrame.add(showButton);
? ? ? ? jFrame.add(exitButton);
? ? ? ? addButton.setVisible(true);
? ? ? ? modifyButton.setVisible(true);
? ? ? ? deleteButton.setVisible(true);
? ? ? ? searchButton.setVisible(true);
? ? ? ? showButton.setVisible(true);
? ? ? ? exitButton.setVisible(true);
? ? ? ? //讀取文件
? ? ? ? try{
? ? ? ? ? ? FileInputStream fileInputStream=new FileInputStream(filename);
? ? ? ? ? ? ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
? ? ? ? ? ? Students students=null;
? ? ? ? ? ? while((students=(Students)objectInputStream.readObject())!=null)
? ? ? ? ? ? ? ? arrayList.add(students);
? ? ? ? ? ? objectInputStream.close();
? ? ? ? ? ? fileInputStream.close();
? ? ? ? }
? ? ? ? catch(Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? //添加學生
? ? public void AddStudent(){
? ? ? ? addButton.addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? //設(shè)置新界面
? ? ? ? ? ? ? ? JDialog addDialog=new JDialog(jFrame);
? ? ? ? ? ? ? ? addDialog.setLayout(new FlowLayout());
? ? ? ? ? ? ? ? //界面標題
? ? ? ? ? ? ? ? addDialog.setTitle("添加學生");
? ? ? ? ? ? ? ? addDialog.setSize(800,700);
? ? ? ? ? ? ? ? addDialog.setLocation(600,200);
? ? ? ? ? ? ? ? //設(shè)置相關(guān)標簽和文本框
? ? ? ? ? ? ? ? JLabel idLabel=new JLabel("學號:");
? ? ? ? ? ? ? ? JTextField idField=new JTextField("");
? ? ? ? ? ? ? ? idField.setPreferredSize(new Dimension(100,50));
? ? ? ? ? ? ? ? JLabel nameLabel=new JLabel("姓名:");
? ? ? ? ? ? ? ? JTextField nameField=new JTextField("");
? ? ? ? ? ? ? ? nameField.setPreferredSize(new Dimension(100,50));
? ? ? ? ? ? ? ? JLabel ageLabel=new JLabel("年齡:");
? ? ? ? ? ? ? ? JTextField ageField=new JTextField("");
? ? ? ? ? ? ? ? ageField.setPreferredSize(new Dimension(100,50));
? ? ? ? ? ? ? ? idLabel.setVisible(true);
? ? ? ? ? ? ? ? idField.setVisible(true);
? ? ? ? ? ? ? ? nameLabel.setVisible(true);
? ? ? ? ? ? ? ? nameField.setVisible(true);
? ? ? ? ? ? ? ? ageLabel.setVisible(true);
? ? ? ? ? ? ? ? ageField.setVisible(true);
? ? ? ? ? ? ? ? //將組件添加進入副界面addDialog
? ? ? ? ? ? ? ? addDialog.add(idLabel);
? ? ? ? ? ? ? ? addDialog.add(idField);
? ? ? ? ? ? ? ? addDialog.add(nameLabel);
? ? ? ? ? ? ? ? addDialog.add(nameField);
? ? ? ? ? ? ? ? addDialog.add(ageLabel);
? ? ? ? ? ? ? ? addDialog.add(ageField);
? ? ? ? ? ? ? ? addDialog.setVisible(true);
? ? ? ? ? ? ? ? //設(shè)置提交按鈕
? ? ? ? ? ? ? ? JButton submitButton=new JButton("確定");
? ? ? ? ? ? ? ? submitButton.setBounds(300,400,200,75);
? ? ? ? ? ? ? ? submitButton.setVisible(true);
? ? ? ? ? ? ? ? addDialog.add(submitButton);
? ? ? ? ? ? ? ? submitButton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? //獲取輸入的內(nèi)容
? ? ? ? ? ? ? ? ? ? ? ? int id=Integer.parseInt(idField.getText());
? ? ? ? ? ? ? ? ? ? ? ? String name=nameField.getText();
? ? ? ? ? ? ? ? ? ? ? ? int age=Integer.parseInt(ageField.getText());
? ? ? ? ? ? ? ? ? ? ? ? Students students=new Students(id,name,age);
? ? ? ? ? ? ? ? ? ? ? ? arrayList.add(students);
? ? ? ? ? ? ? ? ? ? ? ? //提交后返回主頁面
? ? ? ? ? ? ? ? ? ? ? ? addDialog.setVisible(false);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? //設(shè)置返回按鈕
? ? ? ? ? ? ? ? JButton returnbutton=new JButton("返回");
? ? ? ? ? ? ? ? returnbutton.setBounds(300,500,200,75);
? ? ? ? ? ? ? ? returnbutton.setLayout(null);
? ? ? ? ? ? ? ? returnbutton.setVisible(true);
? ? ? ? ? ? ? ? addDialog.add(returnbutton);
? ? ? ? ? ? ? ? returnbutton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? //返回主頁面
? ? ? ? ? ? ? ? ? ? ? ? addDialog.setVisible(false);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? jFrame.add(addDialog);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? //修改學生信息
? ? public void ModifyStudent(){
? ? ? ? modifyButton.addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? //設(shè)置新界面
? ? ? ? ? ? ? ? JDialog modifyDialog=new JDialog(jFrame);
? ? ? ? ? ? ? ? modifyDialog.setTitle("修改學生");
? ? ? ? ? ? ? ? modifyDialog.setSize(800,700);
? ? ? ? ? ? ? ? modifyDialog.setLocation(600,200);
? ? ? ? ? ? ? ? modifyDialog.setLayout(null);
? ? ? ? ? ? ? ? modifyDialog.setVisible(true);
? ? ? ? ? ? ? ? //搜索條件
? ? ? ? ? ? ? ? JLabel namelabel=new JLabel("姓名");
? ? ? ? ? ? ? ? JTextField namefield=new JTextField("");
? ? ? ? ? ? ? ? namelabel.setBounds(100,0,60,40);
? ? ? ? ? ? ? ? namefield.setBounds(160,0,200,40);
? ? ? ? ? ? ? ? namelabel.setLayout(null);
? ? ? ? ? ? ? ? namefield.setLayout(null);
? ? ? ? ? ? ? ? namelabel.setVisible(true);
? ? ? ? ? ? ? ? namefield.setVisible(true);
? ? ? ? ? ? ? ? modifyDialog.add(namelabel);
? ? ? ? ? ? ? ? modifyDialog.add(namefield);
? ? ? ? ? ? ? ? //設(shè)置查詢按鈕
? ? ? ? ? ? ? ? JButton searchbutton=new JButton("查詢");
? ? ? ? ? ? ? ? searchbutton.setBounds(0,0,60,40);
? ? ? ? ? ? ? ? searchbutton.setLayout(null);
? ? ? ? ? ? ? ? searchbutton.setVisible(true);
? ? ? ? ? ? ? ? modifyDialog.add(searchbutton);
? ? ? ? ? ? ? ? searchbutton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? //將符合條件的學生加入哈希表
? ? ? ? ? ? ? ? ? ? ? ? Map<Integer,Students>map=new HashMap<>();
? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if(namefield.getText().equals(arrayList.get(i).getName())){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? //顯示結(jié)果
? ? ? ? ? ? ? ? ? ? ? ? if(map.size()==0)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton resultbutton=new JButton("查無此人");
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setBounds(300,200,200,50);
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.add(resultbutton);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? int num=0;
? ? ? ? ? ? ? ? ? ? ? ? ? ? int height=40;
? ? ? ? ? ? ? ? ? ? ? ? ? ? for(Map.Entry<Integer,Students>entry:map.entrySet()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JLabel idlabel=new JLabel("學號");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JTextField idfield=new JTextField(entry.getValue().getId()+"");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JLabel nameLabel=new JLabel("姓名");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JTextField namefield=new JTextField(entry.getValue().getName());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JLabel ageLabel=new JLabel("年齡");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JTextField agefield=new JTextField(entry.getValue().getAge()+"");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? idlabel.setBounds(60,40+height*num,60,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? idfield.setBounds(120,40+height*num,180,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? namelabel.setBounds(300,40+height*num,60,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? namefield.setBounds(360,40+height*num,190,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ageLabel.setBounds(550,40+height*num,60,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? agefield.setBounds(610,40+height*num,190,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? idlabel.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? idfield.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? nameLabel.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? namefield.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ageLabel.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? agefield.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? idlabel.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? idfield.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? namelabel.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? namefield.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ageLabel.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? agefield.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.add(idlabel);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.add(idfield);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.add(nameLabel);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.add(namefield);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.add(ageLabel);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.add(agefield);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改按鈕
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton yes=new JButton("修改");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? yes.setBounds(0,40+num*height,60,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? yes.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? yes.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? yes.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String tempname=namefield.getText();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempid=Integer.parseInt(idfield.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempage=Integer.parseInt(agefield.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? arrayList.get(entry.getKey()).setName(tempname);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? arrayList.get(entry.getKey()).setId(tempid);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? arrayList.get(entry.getKey()).setAge(tempage);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改后返回主頁面
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.setVisible(false);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.add(yes);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ++num;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? //設(shè)置返回按鈕
? ? ? ? ? ? ? ? JButton returnbutton=new JButton("返回");
? ? ? ? ? ? ? ? returnbutton.setBounds(200,600,200,50);
? ? ? ? ? ? ? ? returnbutton.setLayout(null);
? ? ? ? ? ? ? ? returnbutton.setVisible(true);
? ? ? ? ? ? ? ? modifyDialog.add(returnbutton);
? ? ? ? ? ? ? ? returnbutton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? modifyDialog.setVisible(false);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? jFrame.add(modifyDialog);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? //刪除學生
? ? public void DeleteStudent(){
? ? ? ? deleteButton.addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? //設(shè)置新界面
? ? ? ? ? ? ? ? JDialog deleteDialog=new JDialog(jFrame);
? ? ? ? ? ? ? ? deleteDialog.setTitle("刪除學生");
? ? ? ? ? ? ? ? deleteDialog.setSize(800,700);
? ? ? ? ? ? ? ? deleteDialog.setLocation(600,200);
? ? ? ? ? ? ? ? deleteDialog.setLayout(null);
? ? ? ? ? ? ? ? deleteDialog.setVisible(true);
? ? ? ? ? ? ? ? //搜索條件
? ? ? ? ? ? ? ? JLabel namelabel=new JLabel("姓名");
? ? ? ? ? ? ? ? JTextField namefield=new JTextField("");
? ? ? ? ? ? ? ? namelabel.setBounds(100,0,60,40);
? ? ? ? ? ? ? ? namefield.setBounds(160,0,200,40);
? ? ? ? ? ? ? ? namelabel.setLayout(null);
? ? ? ? ? ? ? ? namefield.setLayout(null);
? ? ? ? ? ? ? ? namelabel.setVisible(true);
? ? ? ? ? ? ? ? namefield.setVisible(true);
? ? ? ? ? ? ? ? deleteDialog.add(namelabel);
? ? ? ? ? ? ? ? deleteDialog.add(namefield);
? ? ? ? ? ? ? ? //設(shè)置查詢按鈕
? ? ? ? ? ? ? ? JButton searchbutton=new JButton("查詢");
? ? ? ? ? ? ? ? searchbutton.setBounds(0,0,60,40);
? ? ? ? ? ? ? ? searchbutton.setLayout(null);
? ? ? ? ? ? ? ? searchbutton.setVisible(true);
? ? ? ? ? ? ? ? searchbutton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? //篩選符合條件的學生
? ? ? ? ? ? ? ? ? ? ? ? Map<Integer,Students>map=new HashMap<>();
? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if(namefield.getText().equals(arrayList.get(i).getName())){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? if(map.size()==0)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton resultbutton=new JButton("查無此人");
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setBounds(300,200,200,50);
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? deleteDialog.add(resultbutton);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? int num=0;
? ? ? ? ? ? ? ? ? ? ? ? ? ? int height=40;
? ? ? ? ? ? ? ? ? ? ? ? ? ? for(Map.Entry<Integer,Students>entry:map.entrySet()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton del=new JButton("刪除");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? del.setBounds(0,40+num*height,60,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? del.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? del.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? del.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? arrayList.remove(entry.getValue());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除后返回主頁面
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? deleteDialog.setVisible(false);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? deleteDialog.add(del);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JTextField resultfield=new JTextField(entry.getValue().toString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? resultfield.setBounds(60,40+height*num,740,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? resultfield.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? resultfield.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? deleteDialog.add(resultfield);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ++num;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? //設(shè)置返回按鈕
? ? ? ? ? ? ? ? JButton returnbutton=new JButton("返回");
? ? ? ? ? ? ? ? returnbutton.setBounds(200,600,200,50);
? ? ? ? ? ? ? ? returnbutton.setLayout(null);
? ? ? ? ? ? ? ? returnbutton.setVisible(true);
? ? ? ? ? ? ? ? deleteDialog.add(returnbutton);
? ? ? ? ? ? ? ? returnbutton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? deleteDialog.setVisible(false);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? deleteDialog.add(searchbutton);
? ? ? ? ? ? ? ? jFrame.add(deleteDialog);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? //查找學生
? ? public void SearchStudent(){
? ? ? ? searchButton.addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? //設(shè)置新界面
? ? ? ? ? ? ? ? JDialog searchDialog=new JDialog(jFrame);
? ? ? ? ? ? ? ? searchDialog.setTitle("查找學生");
? ? ? ? ? ? ? ? searchDialog.setSize(800,700);
? ? ? ? ? ? ? ? searchDialog.setLocation(600,200);
? ? ? ? ? ? ? ? searchDialog.setLayout(null);
? ? ? ? ? ? ? ? searchDialog.setVisible(true);
? ? ? ? ? ? ? ? //設(shè)置相關(guān)標簽和文本框
? ? ? ? ? ? ? ? JLabel idLabel=new JLabel("學號:");
? ? ? ? ? ? ? ? JTextField idField=new JTextField("0");
? ? ? ? ? ? ? ? idField.setPreferredSize(new Dimension(100,50));
? ? ? ? ? ? ? ? JLabel nameLabel=new JLabel("姓名:");
? ? ? ? ? ? ? ? JTextField nameField=new JTextField("");
? ? ? ? ? ? ? ? nameField.setPreferredSize(new Dimension(100,50));
? ? ? ? ? ? ? ? JLabel ageLabel=new JLabel("年齡:");
? ? ? ? ? ? ? ? JTextField ageField=new JTextField("0");
? ? ? ? ? ? ? ? ageField.setPreferredSize(new Dimension(100,50));
? ? ? ? ? ? ? ? int width=250;
? ? ? ? ? ? ? ? int height=50;
? ? ? ? ? ? ? ? idLabel.setBounds(60,0,40,40);
? ? ? ? ? ? ? ? idField.setBounds(100,0,200,40);
? ? ? ? ? ? ? ? nameLabel.setBounds(300,0,50,40);
? ? ? ? ? ? ? ? nameField.setBounds(350,0,200,40);
? ? ? ? ? ? ? ? ageLabel.setBounds(550,0,50,40);
? ? ? ? ? ? ? ? ageField.setBounds(600,0,200,50);
? ? ? ? ? ? ? ? idLabel.setLayout(null);
? ? ? ? ? ? ? ? idField.setLayout(null);
? ? ? ? ? ? ? ? nameLabel.setLayout(null);
? ? ? ? ? ? ? ? nameField.setLayout(null);
? ? ? ? ? ? ? ? ageLabel.setLayout(null);
? ? ? ? ? ? ? ? ageField.setLayout(null);
? ? ? ? ? ? ? ? idLabel.setVisible(true);
? ? ? ? ? ? ? ? idField.setVisible(true);
? ? ? ? ? ? ? ? nameLabel.setVisible(true);
? ? ? ? ? ? ? ? nameField.setVisible(true);
? ? ? ? ? ? ? ? ageLabel.setVisible(true);
? ? ? ? ? ? ? ? ageField.setVisible(true);
? ? ? ? ? ? ? ? //將組件添加進入副界面searchDialog
? ? ? ? ? ? ? ? searchDialog.add(idLabel);
? ? ? ? ? ? ? ? searchDialog.add(idField);
? ? ? ? ? ? ? ? searchDialog.add(nameLabel);
? ? ? ? ? ? ? ? searchDialog.add(nameField);
? ? ? ? ? ? ? ? searchDialog.add(ageLabel);
? ? ? ? ? ? ? ? searchDialog.add(ageField);
? ? ? ? ? ? ? ? //設(shè)置查詢按鈕
? ? ? ? ? ? ? ? JButton searchbutton=new JButton("查詢");
? ? ? ? ? ? ? ? searchbutton.setBounds(0,0,60,40);
? ? ? ? ? ? ? ? searchbutton.setLayout(null);
? ? ? ? ? ? ? ? searchbutton.setVisible(true);
? ? ? ? ? ? ? ? searchDialog.add(searchbutton);
? ? ? ? ? ? ? ? searchbutton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? //根據(jù)查詢條件進行篩選
? ? ? ? ? ? ? ? ? ? ? ? int whichcase=0;
? ? ? ? ? ? ? ? ? ? ? ? if(!nameField.getText().equals(""))
? ? ? ? ? ? ? ? ? ? ? ? ? ? whichcase+=1;
? ? ? ? ? ? ? ? ? ? ? ? if(!idField.getText().equals("0"))
? ? ? ? ? ? ? ? ? ? ? ? ? ? whichcase+=2;
? ? ? ? ? ? ? ? ? ? ? ? if(!ageField.getText().equals("0"))
? ? ? ? ? ? ? ? ? ? ? ? ? ? whichcase+=4;
? ? ? ? ? ? ? ? ? ? ? ? System.out.println(nameField.getText()+" "+idField.getText()+" "+ageField.getText());
? ? ? ? ? ? ? ? ? ? ? ? //存儲查詢結(jié)果
? ? ? ? ? ? ? ? ? ? ? ? Map<Integer,Students>map=new HashMap<>();
? ? ? ? ? ? ? ? ? ? ? ? switch(whichcase){
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// System.out.println(tempname);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(nameField.getText().equals(arrayList.get(i).getName()))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempid=Integer.parseInt(idField.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(tempid==arrayList.get(i).getId())
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String tempname=nameField.getText();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempid=Integer.parseInt(idField.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(tempname.equals(arrayList.get(i).getName())&&tempid==arrayList.get(i).getId())
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempage=Integer.parseInt(ageField.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(tempage==arrayList.get(i).getAge())
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String tempname=nameField.getText();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempage=Integer.parseInt(ageField.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(tempname.equals(arrayList.get(i).getName())&&tempage==arrayList.get(i).getAge())
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempid=Integer.parseInt(idField.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempage=Integer.parseInt(ageField.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(tempid==arrayList.get(i).getId()&&tempage==arrayList.get(i).getAge()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? case 7:
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String tempname=nameField.getText();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempid=Integer.parseInt(idField.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int tempage=Integer.parseInt(ageField.getText());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int i=0;i<arrayList.size();++i)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(tempname.equals(arrayList.get(i).getName())&&tempid==arrayList.get(i).getId()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? &&tempage==arrayList.get(i).getAge()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? map.put(i,arrayList.get(i));
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? int num=0;
? ? ? ? ? ? ? ? ? ? ? ? for(Map.Entry<Integer,Students>entry:map.entrySet()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println(entry.getValue().toString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton tempindexbutton=new JButton(entry.getKey()+"");
? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton tempidbutton=new JButton(entry.getValue().getId()+"");
? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton tempnamebutton=new JButton(entry.getValue().getName());
? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton tempagebutton=new JButton(entry.getValue().getAge()+"");
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempindexbutton.setBounds(0,40+height*num,50,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempidbutton.setBounds(50,40+height*num,width,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempnamebutton.setBounds(300,40+height*num,width,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempagebutton.setBounds(550,40+height*num,width,height);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempindexbutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempidbutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempnamebutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempagebutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempindexbutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempidbutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempnamebutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempagebutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? searchDialog.add(tempindexbutton);
? ? ? ? ? ? ? ? ? ? ? ? ? ? searchDialog.add(tempidbutton);
? ? ? ? ? ? ? ? ? ? ? ? ? ? searchDialog.add(tempnamebutton);
? ? ? ? ? ? ? ? ? ? ? ? ? ? searchDialog.add(tempagebutton);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ++num;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? if(map.size()==0)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("查無此人");
? ? ? ? ? ? ? ? ? ? ? ? ? ? JButton resultbutton=new JButton("查無此人");
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setBounds(300,200,200,50);
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? resultbutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? searchDialog.add(resultbutton);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? //設(shè)置返回按鈕
? ? ? ? ? ? ? ? //設(shè)置返回按鈕
? ? ? ? ? ? ? ? JButton returnbutton=new JButton("返回");
? ? ? ? ? ? ? ? returnbutton.setBounds(200,600,width,height);
? ? ? ? ? ? ? ? returnbutton.setLayout(null);
? ? ? ? ? ? ? ? returnbutton.setVisible(true);
? ? ? ? ? ? ? ? searchDialog.add(returnbutton);
? ? ? ? ? ? ? ? returnbutton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? ? ? ? ? searchDialog.setVisible(false);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? //顯示學生
? ? public void ShowStudent(){
? ? ? ? showButton.addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? //設(shè)置新界面
? ? ? ? ? ? ? ? JDialog showDialog=new JDialog(jFrame);
? ? ? ? ? ? ? ? showDialog.setTitle("顯示學生");
? ? ? ? ? ? ? ? showDialog.setSize(800,700);
? ? ? ? ? ? ? ? showDialog.setLocation(600,200);
? ? ? ? ? ? ? ? showDialog.setLayout(null);
? ? ? ? ? ? ? ? showDialog.setVisible(true);
? ? ? ? ? ? ? ? JButton idbutton=new JButton("id");
? ? ? ? ? ? ? ? idbutton.setBounds(50,0,250,50);
? ? ? ? ? ? ? ? idbutton.setLayout(null);
? ? ? ? ? ? ? ? JButton namebutton=new JButton("name");
? ? ? ? ? ? ? ? namebutton.setBounds(300,0,250,50);
? ? ? ? ? ? ? ? namebutton.setLayout(null);
? ? ? ? ? ? ? ? JButton agebutton=new JButton("age");
? ? ? ? ? ? ? ? agebutton.setBounds(550,0,250,50);
? ? ? ? ? ? ? ? agebutton.setLayout(null);
? ? ? ? ? ? ? ? idbutton.setVisible(true);
? ? ? ? ? ? ? ? namebutton.setVisible(true);
? ? ? ? ? ? ? ? agebutton.setVisible(true);
? ? ? ? ? ? ? ? showDialog.add(idbutton);
? ? ? ? ? ? ? ? showDialog.add(namebutton);
? ? ? ? ? ? ? ? showDialog.add(agebutton);
? ? ? ? ? ? ? ? //數(shù)量
? ? ? ? ? ? ? ? int num=0;
? ? ? ? ? ? ? ? int height=40;
? ? ? ? ? ? ? ? int width=250;
? ? ? ? ? ? ? ? while(num<arrayList.size()){
? ? ? ? ? ? ? ? ? ? //設(shè)置按鈕來顯示數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? JButton numbutton=new JButton(num+"");
? ? ? ? ? ? ? ? ? ? JButton tempidbutton=new JButton(arrayList.get(num).getId()+"");
? ? ? ? ? ? ? ? ? ? JButton tempnamebutton=new JButton(arrayList.get(num).getName());
? ? ? ? ? ? ? ? ? ? JButton tempagebutton=new JButton(arrayList.get(num).getAge()+"");
? ? ? ? ? ? ? ? ? ? numbutton.setBounds(0,50+height*num,50,height);
? ? ? ? ? ? ? ? ? ? tempidbutton.setBounds(50,50+height*num,width,height);
? ? ? ? ? ? ? ? ? ? tempnamebutton.setBounds(300,50+height*num,width,height);
? ? ? ? ? ? ? ? ? ? tempagebutton.setBounds(550,50+height*num,width,height);
? ? ? ? ? ? ? ? ? ? numbutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? tempidbutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? tempnamebutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? tempagebutton.setLayout(null);
? ? ? ? ? ? ? ? ? ? numbutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? tempidbutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? tempnamebutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? tempagebutton.setVisible(true);
? ? ? ? ? ? ? ? ? ? showDialog.add(numbutton);
? ? ? ? ? ? ? ? ? ? showDialog.add(tempidbutton);
? ? ? ? ? ? ? ? ? ? showDialog.add(tempnamebutton);
? ? ? ? ? ? ? ? ? ? showDialog.add(tempagebutton);
? ? ? ? ? ? ? ? ? ? ++num;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //設(shè)置返回按鈕
? ? ? ? ? ? ? ? JButton returnbutton=new JButton("返回");
? ? ? ? ? ? ? ? returnbutton.setBounds(200,600,width,height);
? ? ? ? ? ? ? ? returnbutton.setLayout(null);
? ? ? ? ? ? ? ? returnbutton.setVisible(true);
? ? ? ? ? ? ? ? showDialog.add(returnbutton);
? ? ? ? ? ? ? ? returnbutton.addActionListener(new ActionListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void actionPerformed(ActionEvent e) {

? ? ? ? ? ? ? ? ? ? ? ? showDialog.setVisible(false);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? jFrame.add(showDialog);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? //退出系統(tǒng)
? ? public void ExitButton(){
? ? ? ? exitButton.addActionListener(new ActionListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void actionPerformed(ActionEvent e) {
? ? ? ? ? ? ? ? //前期的添加,刪除,修改等工作,都只作用于ArrayList,最后退出系統(tǒng)時,再更新文件
? ? ? ? ? ? ? ? try{
? ? ? ? ? ? ? ? ? ? FileOutputStream fileOutputStream=new FileOutputStream(filename);
? ? ? ? ? ? ? ? ? ? ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
? ? ? ? ? ? ? ? ? ? for(Students stu:arrayList)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? objectOutputStream.writeObject(stu);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? objectOutputStream.close();
? ? ? ? ? ? ? ? ? ? fileOutputStream.close();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch(Exception ex){
? ? ? ? ? ? ? ? ? ? ex.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? jFrame.setVisible(false);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? //菜單
? ? public void Menu(){
? ? ? ? //添加監(jiān)聽器
? ? ? ? this.AddStudent();
? ? ? ? this.ModifyStudent();
? ? ? ? this.DeleteStudent();
? ? ? ? this.SearchStudent();
? ? ? ? this.ShowStudent();
? ? ? ? this.ExitButton();
? ? ? ? jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
? ? ? ? jFrame.setVisible(true);
? ? }
}
public class hello {
? ? public static void main(String[]args)
? ? {
? ? ? ?ManageSystem manageSystem=new ManageSystem();
? ? ? ?manageSystem.Menu();
? ? }
}

效果圖:

1.主頁面

2.添加學生頁面

3.查找學生頁面

4.總結(jié)

該圖形化界面核心代碼其實和第一個在控制臺操作的管理系統(tǒng)基本一樣,只是進行圖形化,使操作更方便而已,但是在圖形化的時候,發(fā)現(xiàn)有許多bug,首先就是每次操作后,必須按退出系統(tǒng)按鈕,進行的操作才能寫入文件。其次就是刪除,查找和修改頁面,輸入查詢條件后,鼠標必須經(jīng)過相應的區(qū)域,該區(qū)域上的結(jié)果才會顯示出來。最后還有一點,就是重復的代碼太多了,可以將一些代碼封裝成一個函數(shù),需要時再調(diào)用。

3.學生管理系統(tǒng)(連接MySQL數(shù)據(jù)庫)

1.打開CMD,要以管理員的身份運行

cd "Mysql所在文件夾"
//啟動mysql
net start mysql

//登錄MySQL
mysql -u root -p

然后輸入密碼

//創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE shop;

創(chuàng)建shop數(shù)據(jù)庫中的表

在表中插入信息

在MySQL進行上述操作后,編寫下面的JAVA代碼

package project.demo;
import java.sql.*;
import java.util.ArrayList;
import java.util.*;
import java.io.*;
import ?java.io.*;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
class ManageSystem
{
? ? //驅(qū)動名,Java8及以上都是這個
? ? private String driver="com.mysql.cj.jdbc.Driver";
? ? //數(shù)據(jù)庫用戶名
? ? private String user="root";
? ? //數(shù)據(jù)庫密碼
? ? private String password="3fa4d180";
? ? //要使用的數(shù)據(jù)庫
? ? private String database="shop";
? ? //數(shù)據(jù)庫路徑
? ? private String url=null;
? ? public ManageSystem(){
? ? ? ? //初始化數(shù)據(jù)庫路徑
? ? ? ? url="jdbc:mysql://localhost:3306/"+database+"?useSSL=false&serverTimezone=UTC";
? ? }
? ? public void Menu(){
? ? ? ? boolean flag=true;
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? while(flag)
? ? ? ? {
? ? ? ? ? ? System.out.println("----------------------------------");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|");
? ? ? ? ? ? System.out.println("|-------------1.添加學生-----------");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|");
? ? ? ? ? ? System.out.println("|-------------2.修改學生-----------");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|");
? ? ? ? ? ? System.out.println("|-------------3.刪除學生-----------");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|");
? ? ? ? ? ? System.out.println("|-------------4.查找學生-----------");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|");
? ? ? ? ? ? System.out.println("|-------------5.顯示學生-----------");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|");
? ? ? ? ? ? System.out.println("|-------------6.退出系統(tǒng)-----------");
? ? ? ? ? ? System.out.println("| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|");
? ? ? ? ? ? System.out.println("|---------------------------------");
? ? ? ? ? ? System.out.print("請輸入您的選擇:");
? ? ? ? ? ? //輸入選擇
? ? ? ? ? ? int choice=input.nextInt();
? ? ? ? ? ? switch(choice)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? ? ? this.AddStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? ? ? this.ModifyStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? ? ? this.DeleteStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? ? ? this.SearchStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? ? ? this.ShowStudent();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case 6:
? ? ? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? System.out.println("歡迎下次使用");
? ? }
? ? //添加學生
? ? public void AddStudent(){
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? try{
? ? ? ? ? ? //加載驅(qū)動
? ? ? ? ? ? Class.forName(driver);
? ? ? ? ? ? //連接到數(shù)據(jù)庫
? ? ? ? ? ? Connection conn=DriverManager.getConnection(url,user,password);
? ? ? ? ? ? System.out.println("連接成功");
? ? ? ? ? ? //設(shè)置容器
? ? ? ? ? ? Statement stmt=conn.createStatement();
? ? ? ? ? ? boolean flag=true;
? ? ? ? ? ? while(flag)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? System.out.print("請輸入學生id:");
? ? ? ? ? ? ? ? int id=input.nextInt();
? ? ? ? ? ? ? ? input=new Scanner(System.in);
? ? ? ? ? ? ? ? System.out.print("請輸入學生姓名:");
? ? ? ? ? ? ? ? String name=input.nextLine();
? ? ? ? ? ? ? ? System.out.print("請輸入學生年齡:");
? ? ? ? ? ? ? ? int age=input.nextInt();
? ? ? ? ? ? ? ? //SQL語句
? ? ? ? ? ? ? ? String execute="INSERT INTO Student VALUES";
? ? ? ? ? ? ? ? //在Mysql的添加語句中,若添加id=202004,name=劉六,age=20,添加語句為INSERT INTO Student VALUES('202004','劉六',20);
? ? ? ? ? ? ? ? //即字符需要用"'"包起來
? ? ? ? ? ? ? ? execute+=("("+"'"+id+"'"+","+"'"+name+"'"+","+age+");");
? ? ? ? ? ? ? ? //將SQL語句上傳到數(shù)據(jù)庫執(zhí)行
? ? ? ? ? ? ? ? stmt.executeUpdate(execute);
? ? ? ? ? ? ? ? System.out.println("插入成功");
? ? ? ? ? ? ? ? System.out.println("是否繼續(xù)插入數(shù)據(jù)?");
? ? ? ? ? ? ? ? System.out.println("1.是");
? ? ? ? ? ? ? ? System.out.println("2.否");
? ? ? ? ? ? ? ? System.out.print("請輸入您的選擇:");
? ? ? ? ? ? ? ? int choice=input.nextInt();
? ? ? ? ? ? ? ? if(choice!=1)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //關(guān)閉容器和通道
? ? ? ? ? ? stmt.close();
? ? ? ? ? ? conn.close();
? ? ? ? }catch(ClassNotFoundException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? catch(SQLException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? //停頓1秒,方便用戶查看結(jié)果
? ? ? ? try{
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? //修改學生
? ? public void ModifyStudent(){
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? System.out.print("請輸入要修改的學生姓名:");
? ? ? ? String name=input.nextLine();
? ? ? ? System.out.println("請輸入要修改的內(nèi)容:");
? ? ? ? System.out.println("1.id");
? ? ? ? System.out.println("2.name");
? ? ? ? System.out.println("3.age");
? ? ? ? System.out.println("4.all");
? ? ? ? System.out.print("請輸入您的選擇:");
? ? ? ? int choice=input.nextInt();
? ? ? ? //SQL語句
? ? ? ? String execute="";
? ? ? ? boolean flag=true;
? ? ? ? while(flag){
? ? ? ? ? ? if(choice<1||choice>4)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? System.out.println("輸入有誤,請重新輸入");
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? ? ? if(choice==1){
? ? ? ? ? ? ? ? ? ? System.out.print("請輸入新id:");
? ? ? ? ? ? ? ? ? ? int newid=input.nextInt();
? ? ? ? ? ? ? ? ? ? execute="UPDATE Student SET stu_id="+"'"+newid+"'";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(choice==2){
? ? ? ? ? ? ? ? ? ? input=new Scanner(System.in);
? ? ? ? ? ? ? ? ? ? System.out.print("請輸入新姓名:");
? ? ? ? ? ? ? ? ? ? String newname=input.nextLine();
? ? ? ? ? ? ? ? ? ? execute="UPDATE Student SET stu_name="+"'"+newname+"'";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(choice==3){
? ? ? ? ? ? ? ? ? ? System.out.print("請輸入新年齡:");
? ? ? ? ? ? ? ? ? ? int newage=input.nextInt();
? ? ? ? ? ? ? ? ? ? execute="UPDATE Student SET stu_age="+"'"+newage+"'";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? System.out.print("請輸入新id:");
? ? ? ? ? ? ? ? ? ? int newid=input.nextInt();
? ? ? ? ? ? ? ? ? ? input=new Scanner(System.in);
? ? ? ? ? ? ? ? ? ? System.out.print("請輸入新姓名:");
? ? ? ? ? ? ? ? ? ? String newname=input.nextLine();
? ? ? ? ? ? ? ? ? ? System.out.print("請輸入新年齡:");
? ? ? ? ? ? ? ? ? ? int newage=input.nextInt();
? ? ? ? ? ? ? ? ? ? execute="UPDATE Student SET stu_id="+"'"+newid+"'";
? ? ? ? ? ? ? ? ? ? execute+=",stu_name="+"'"+newname+"'";
? ? ? ? ? ? ? ? ? ? execute+=",stu_age="+"'"+newage+"'";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? execute+=" WHERE stu_name="+"'"+name+"'"+";";
? ? ? ? try{
? ? ? ? ? ? //設(shè)置驅(qū)動
? ? ? ? ? ? Class.forName(driver);
? ? ? ? ? ? //連接數(shù)據(jù)庫
? ? ? ? ? ? Connection conn=DriverManager.getConnection(url,user,password);
? ? ? ? ? ? //設(shè)置容器
? ? ? ? ? ? Statement stmt=conn.createStatement();
? ? ? ? ? ? //執(zhí)行SQL語句
? ? ? ? ? ? stmt.executeUpdate(execute);
? ? ? ? ? ? System.out.println("修改成功");
? ? ? ? ? ? //關(guān)閉容器和數(shù)據(jù)庫
? ? ? ? ? ? stmt.close();
? ? ? ? ? ? conn.close();
? ? ? ? }
? ? ? ? catch(ClassNotFoundException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? catch(SQLException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? //停頓1秒,方便用戶觀察結(jié)果
? ? ? ? try{
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch(Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? //刪除學生
? ? public void DeleteStudent(){
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? System.out.println("請輸入要刪除的學生姓名:");
? ? ? ? String name=input.nextLine();
? ? ? ? try{
? ? ? ? ? ? //加載驅(qū)動
? ? ? ? ? ? Class.forName(driver);
? ? ? ? ? ? //連接數(shù)據(jù)庫
? ? ? ? ? ? Connection conn=DriverManager.getConnection(url,user,password);
? ? ? ? ? ? //設(shè)置容器
? ? ? ? ? ? Statement stmt=conn.createStatement();
? ? ? ? ? ? //SQL語句
? ? ? ? ? ? String exceute="DELETE FROM Student WHERE ";
? ? ? ? ? ? exceute+="stu_name="+"'"+name+"'"+";";
? ? ? ? ? ? //執(zhí)行SQL語句
? ? ? ? ? ? stmt.executeUpdate(exceute);
? ? ? ? ? ? System.out.println("刪除成功");
? ? ? ? ? ? //關(guān)閉容器和數(shù)據(jù)庫
? ? ? ? ? ? stmt.close();
? ? ? ? ? ? conn.close();
? ? ? ? }
? ? ? ? catch(ClassNotFoundException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? catch(SQLException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? //停頓1秒,方便用戶觀察
? ? ? ? try{
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch(Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? //查找學生
? ? public void SearchStudent(){
? ? ? ? Scanner input=new Scanner(System.in);
? ? ? ? System.out.println("請輸入查詢條件");
? ? ? ? System.out.println("1.id");
? ? ? ? System.out.println("2.name");
? ? ? ? System.out.println("3.age");
? ? ? ? System.out.print("請輸入您的選擇:");
? ? ? ? String execute="";
? ? ? ? boolean flag=true;
? ? ? ? while(flag){
? ? ? ? ? ? int choice=input.nextInt();
? ? ? ? ? ? if(choice<1||choice>3)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? System.out.println("輸入有誤,請重新輸入");
? ? ? ? ? ? }
? ? ? ? ? ? else if(choice==1){
? ? ? ? ? ? ? ? System.out.print("請輸入要查詢的id:");
? ? ? ? ? ? ? ? int id=input.nextInt();
? ? ? ? ? ? ? ? execute="SELECT * FROM Student WHERE stu_id="+"'"+id+"'"+";";
? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? }
? ? ? ? ? ? else if(choice==2){
? ? ? ? ? ? ? ? input=new Scanner(System.in);
? ? ? ? ? ? ? ? System.out.print("請輸入要查詢的姓名:");
? ? ? ? ? ? ? ? String name=input.nextLine();
? ? ? ? ? ? ? ? execute="SELECT * FROM Student WHERE stu_name="+"'"+name+"'"+";";
? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? System.out.print("請輸入要查詢的年齡:");
? ? ? ? ? ? ? ? int age=input.nextInt();
? ? ? ? ? ? ? ? execute="SELECT * FROM Student WHERE stu_age="+age+";";
? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? try{
? ? ? ? ? ? //加載驅(qū)動
? ? ? ? ? ? Class.forName(driver);
? ? ? ? ? ? //連接數(shù)據(jù)庫
? ? ? ? ? ? Connection conn=DriverManager.getConnection(url,user,password);
? ? ? ? ? ? //設(shè)置容器
? ? ? ? ? ? Statement stmt=conn.createStatement();
? ? ? ? ? ? //獲得集合
? ? ? ? ? ? ResultSet rs=stmt.executeQuery(execute);
? ? ? ? ? ? int num=0;
? ? ? ? ? ? //遍歷
? ? ? ? ? ? while(rs.next())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int id=Integer.parseInt(rs.getString("stu_id"));
? ? ? ? ? ? ? ? String name=rs.getString("stu_name");
? ? ? ? ? ? ? ? int age=Integer.parseInt(rs.getString("stu_age"));
? ? ? ? ? ? ? ? System.out.println(String.format("id:%-10d name:%-20s age:%-5d",id,name,age));
? ? ? ? ? ? ? ? num++;
? ? ? ? ? ? }
? ? ? ? ? ? if(num!=0)
? ? ? ? ? ? ? ? System.out.println("查詢成功");
? ? ? ? ? ? else
? ? ? ? ? ? ? ? System.out.println("查無此人");
? ? ? ? ? ? //關(guān)閉集合,容器和數(shù)據(jù)庫
? ? ? ? ? ? rs.close();
? ? ? ? ? ? stmt.close();
? ? ? ? ? ? conn.close();
? ? ? ? }
? ? ? ? catch(ClassNotFoundException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? catch(SQLException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? //停頓1秒,方便用戶觀察
? ? ? ? try{
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? //顯示學生
? ? public void ShowStudent(){
? ? ? ? try{
? ? ? ? ? ? //加載驅(qū)動
? ? ? ? ? ? Class.forName(driver);
? ? ? ? ? ? //連接數(shù)據(jù)庫
? ? ? ? ? ? Connection conn=DriverManager.getConnection(url,user,password);
? ? ? ? ? ? System.out.println("連接成功");
? ? ? ? ? ? //設(shè)置容器
? ? ? ? ? ? Statement stmt=conn.createStatement();
? ? ? ? ? ? //SQL語句
? ? ? ? ? ? String execute="SELECT * FROM Student";
? ? ? ? ? ? //獲得集合
? ? ? ? ? ? ResultSet rs= stmt.executeQuery(execute);
? ? ? ? ? ? System.out.println("查詢成功");
? ? ? ? ? ? while(rs.next()){
? ? ? ? ? ? ? ? int id=Integer.parseInt(rs.getString("stu_id"));
? ? ? ? ? ? ? ? String name=rs.getString("stu_name");
? ? ? ? ? ? ? ? int age=Integer.parseInt(rs.getString("stu_age"));
? ? ? ? ? ? ? ? System.out.println(String.format("id:%-10d name:%-20s age:%-5d",id,name,age));
? ? ? ? ? ? }
? ? ? ? ? ? //關(guān)閉集合,容器和數(shù)據(jù)庫
? ? ? ? ? ? rs.close();
? ? ? ? ? ? stmt.close();
? ? ? ? ? ? conn.close();
? ? ? ? }
? ? ? ? catch(ClassNotFoundException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? catch(SQLException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? //停頓1秒,方便用戶觀察
? ? ? ? try{
? ? ? ? ? ? Thread.sleep(1000);
? ? ? ? }
? ? ? ? catch(Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}
public class test{
? ? public static void main(String[]args){
? ? ? ManageSystem manageSystem=new ManageSystem();
? ? ? manageSystem.Menu();
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Servlet Filter過濾器執(zhí)行順序

    Servlet Filter過濾器執(zhí)行順序

    這篇文章主要介紹了Servlet Filter過濾器執(zhí)行順序的相關(guān)資料,幫助大家更好的理解為什么要用過濾器,感興趣的朋友可以了解下
    2020-12-12
  • Spring boot調(diào)用Oracle存儲過程的兩種方式及完整代碼

    Spring boot調(diào)用Oracle存儲過程的兩種方式及完整代碼

    這篇文章主要給大家介紹了關(guān)于Spring boot調(diào)用Oracle存儲過程的兩種方式及完整代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2020-08-08
  • Windows系統(tǒng)下JDK1.8與JDK11版本切換超詳細教程

    Windows系統(tǒng)下JDK1.8與JDK11版本切換超詳細教程

    這篇文章主要給大家介紹了關(guān)于Windows系統(tǒng)下JDK1.8與JDK11版本切換的超詳細教程,我們可以有多個工程項目,用的JDK版本不一樣,這個時候就需要進行自由切換JDK版本了,需要的朋友可以參考下
    2023-07-07
  • Java實戰(zhàn)之兼職平臺系統(tǒng)的實現(xiàn)

    Java實戰(zhàn)之兼職平臺系統(tǒng)的實現(xiàn)

    這篇文章主要介紹了如何利用Java編寫一個兼職平臺系統(tǒng),采用到的技術(shù)有Springboot、SpringMVC、MyBatis、ThymeLeaf等,感興趣的小伙伴可以了解一下
    2022-03-03
  • Spring?Boot的幾種統(tǒng)一處理方式梳理小結(jié)

    Spring?Boot的幾種統(tǒng)一處理方式梳理小結(jié)

    這篇文章主要為大家介紹了Spring?Boot的幾種統(tǒng)一處理方式梳理小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • Java設(shè)計模式之創(chuàng)建者模式詳解

    Java設(shè)計模式之創(chuàng)建者模式詳解

    這篇文章主要介紹了Java設(shè)計模式之創(chuàng)建者模式詳解,創(chuàng)建者模式,顧名思義,就是提供友好的創(chuàng)建對象的方式?,對象都是?new?出來的,但是在一些情況下,這種方式不是很友好,首先,它不夠直觀,需要的朋友可以參考下
    2023-08-08
  • 解決idea每次新建項目都需要重新指定maven目錄

    解決idea每次新建項目都需要重新指定maven目錄

    這篇文章主要介紹了解決idea每次新建項目都需要配置maven,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 如何在IDEA中快速解決Jar沖突詳解

    如何在IDEA中快速解決Jar沖突詳解

    相信很多同學在過去做項目都遇到過Jar沖突的問題,在本地環(huán)境沒問題,一旦部署到測試或生產(chǎn)環(huán)境突然就啟動報錯,報類似classNotFound的Exception,本文詳細整理了如何在IDEA中快速解決Jar沖突,需要的朋友可以參考下
    2021-06-06
  • RabbitMQ 的七種隊列模式和應用場景

    RabbitMQ 的七種隊列模式和應用場景

    最近學習RabbitMQ,本文就記錄一下RabbitMQ 的七種隊列模式和應用場景,方便以后使用,也方便和大家共享,相互交流
    2021-05-05
  • Springboot mybatis-plus配置及用法詳解

    Springboot mybatis-plus配置及用法詳解

    這篇文章主要介紹了Springboot mybatis-plus配置及用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09

最新評論