1. 程式人生 > >mysql資料庫程式實現班級管理系統(GradeMangerSystem_MVC_V1.1)及mvc分層

mysql資料庫程式實現班級管理系統(GradeMangerSystem_MVC_V1.1)及mvc分層

 MVC軟體架構模式:

view檢視層:負責與使用者進行互動,介面顯示

model模型層:業務處理,持久儲存資料

contrl控制層:負責接收介面資料組裝傳給model處理,獲取model層顯示到介面。

bean類表示java實體:

1.所有屬性私有化,

2.每個屬性對應公共的get()和set()方法

3.可以定義多個構造方法

專案名稱: 《班級管理系統》 英文:GradeMangerSystem

功能描術: 班級管理系統是對班級學生進行管理的一套系統,

         具體包含學生管理、班級管理、學生選班、學生入班等功能.         

要求:

 1>.建立一個班級類Grade至少包含:班級名稱(name)、編號(id),班級所有學生(students)三個屬性;

        在Grade類中封裝班級管理功能: 建立班級(addGrade)、顯示班級(showAllClass)、

               顯示班級所有學生(showAllStudentByGrade)

 2>.建立學生類Student要求與學生資訊管理系統相同.

 3>.選擇班級不存在時需給出提示。

 3>.通過輸入命令進行互動操作,控制檯輸出效果如下:

 ======= 歡迎進入班級管理系統! ======

 =====【班級管理系統主介面】=======

 1 班級管理, 2 學生管理,  3 退出

------------------1班級管理-----------------------------

<輸入操作命令:>

1

======班級管理操作=========

1 建立班級, 2 顯示班級, 3 檢視班級學生, 4返回班級系統主介面

------------------1 建立班級-----------------------------

<輸入操作命令:>

1

請輸入班級名稱:

java班

請輸入班級編號:

1001

======新增成功 ! 班級名是 :Java班,編號是 : 1001=========

------------------2顯示班級-----------------------------

<輸入操作命令:>

2

班級         編號

java班       1001

IOS         1002

------------------3根據班級編號檢視班級學生---------------------

<輸入操作命令:>

3

請輸入班級名稱:

Java班

班級         學生   年齡   性別

java班       張三   23      男

java班       王二   24      男

java班       小明   23      男

------------------4返回班級系統主介面--------------------

<輸入操作命令:>

4

 =====【班級管理系統主介面】=======

 1 班級管理, 2 學生管理, 3 退出

------------------2學生管理---------------------------

<輸入操作命令:>

2

=====學生管理主介面========

1 新增學生, 2 顯示學生 , 3 查詢學生 , 4 返回班級系統主介面

------------------1新增學生---------------------------

<輸入操作命令:>

1

請輸入姓名:

小麗

請輸入學號:

10

請輸入年齡:

25

請輸入性別:

請輸入班級名稱:

java班

======新增成功 ! 學生姓名是 :小麗,學號是 :10,選擇班級是java班= ========

 ------------------2 顯示學生----------------------------

<輸入操作命令:>

2

姓名     學號     年齡  性別   班級

小明     10       23     男   java班

莉莉     21       24     女   IOS班

張三     11       19     男   java班

 ------------------3 查詢學生----------------------------

<輸入操作命令:>

3

請輸入學生姓名:

莉莉

姓名     學號     年齡  性別   班級

莉莉     21       24     女   IOS班

------------------4返回班級系統主介面--------------------

<輸入操作命令:>

4

 =====【班級管理系統主介面】=======

 1 班級管理, 2 學生管理, 3 退出

-------------------- 3 退出-------------------

<輸入操作命令:>

3

==== 您已經退出《班級管理系統》 ====

package cn.sxb.day11;

import cn.sxb.day11.KeyBoardUtil;
import cn.sxb.day11.Student;

public class Student {
	String name;
	String gname;//班級編號
	int number;
	int age;
	int sex;
	
	public Student(String name, String gname, int number, int age, int sex) {
		this.name = name;
		this.gname = gname;
		this.number = number;
		this.age = age;
		this.sex = sex;
	}
	
	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getGname() {
		return gname;
	}


	public void setGname(String gname) {
		this.gname = gname;
	}


	public int getNumber() {
		return number;
	}


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


	public int getAge() {
		return age;
	}


	public void setAge(int age) {
		this.age = age;
	}


	public int getSex() {
		return sex;
	}


	public void setSex(int sex) {
		this.sex = sex;
	}
}

package cn.sxb.day11;

public class Grade {
	String gradeName;
	int gradeNumber;//班級編號
	
	public Grade(String gradeName, int gradeNumber) {
		this.gradeName = gradeName;
		this.gradeNumber = gradeNumber;
	}
}

package cn.sxb.day11;

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

public class UtilDB {
	private static final String URL = "jdbc:mysql://192.168.5.17:3306/db1606";
	private static final String USER = "test";
	private static final String PASSWORD = "123321";

	/**
	 * 構造方法載入驅動
	 */
	public UtilDB() {
		try {
			Class.forName("com.mysql.jdbc.Driver");// 載入mysql驅動
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 建立連線資料庫
	 */
	public Connection getConnection() {
		Connection connect = null;
		try {
			connect = DriverManager.getConnection(URL, USER, PASSWORD);// 試圖建立到給定資料庫URL的連線
//			if (connect == null) {
//				System.out.println("資料庫連線失敗");
//			} else {
//				System.out.println("資料庫連線成功");
//			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connect;
	}

	/**
	 * 關閉資料庫連線
	 */
	public void closeConnection(Connection connect) {
		try {
			if (connect != null)
				connect.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

package cn.sxb.day11;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class KeyBoardUtil {
	/**
	 * 使用io流,獲取從鍵盤輸入到程式。使用Input(從外部裝置到程式為輸入,程式到外部裝置為輸出);
	 * System.in(out)標準系統輸入輸出中,不需要關閉流,由系統自動關閉。否則在後面測試類中會回報流關閉錯誤
	 */
	public static String getStringKeyBoard() {
		String name = "";
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			isr = new InputStreamReader(System.in);
			br = new BufferedReader(isr);
			name = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return name;
	}

	public static int getintKeyBoard() {
		while (true) {
			int number = 0;
			InputStreamReader isr = null;
			BufferedReader br = null;
			try {
				isr = new InputStreamReader(System.in);
				br = new BufferedReader(isr);
				String s = br.readLine();// 在緩衝流中用readLine(),每行讀成String型。在轉換成int型
				number = Integer.parseInt(s);

				return number;

			} catch (IOException e) {
				e.printStackTrace();
			} catch (NumberFormatException a) {
				System.out.println("請輸入正確數值格式");
			}
		}
	}
}

package cn.sxb.day11;

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

import cn.sxb.day11.KeyBoardUtil;
import cn.sxb.day11.Student;

public class StudentDemo extends UtilDB{

	public static void main(String[] args) {
		StudentDemo sd = new StudentDemo();
//		sd.createGradeTable();
//		sd.addStudent(sd.addStudentMessageByKeyBoard());
//		sd.showAllStudentMessage();
//		sd.findStudentMessageByName();
	}
	
	/**
	 * 建立stu表
	 */
	public void createGradeTable() {
		Connection connect = null;
		Statement statement = null;
		String sql = "create table  stu (" + "number int(11) not null primary key," // 主鍵
				+ "name varchar(10) not null," // 姓名
				+ "gname varchar(10)  not null," // 班級名
				+ "age int(3) not null," // 年齡
				+ "sex int(1) not null" // 性別
				+ ")";
		try {
			connect = getConnection();
			statement = connect.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (statement != null)
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		closeConnection(connect);
	}
	/**
	 * 鍵盤新增學生資訊
	 */
	public Student addStudentMessageByKeyBoard(){
		System.out.println("請輸入學生姓名:");
		String name = KeyBoardUtil.getStringKeyBoard();
		System.out.println("請輸入學生班級:");
		String gname = KeyBoardUtil.getStringKeyBoard();
		System.out.println("請輸入學生學號:");
		int number = KeyBoardUtil.getintKeyBoard();
		System.out.println("請輸入學生年齡:");
		int age = KeyBoardUtil.getintKeyBoard();
		System.out.println("請輸入學生性別:");
		int sex = KeyBoardUtil.getintKeyBoard();
		
		Student student = new Student(name,gname,number,age,sex);
		return student;
	}
	
	/**
	 * 向stu表中新增學生
	 */
	public void addStudent(Student student){
		String sql = "insert into stu values(?,?,?,?,?)";
		Connection connect = null;
		PreparedStatement ps = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);

			ps.setString(2, student.getName());
			ps.setString(3, student.getGname());
			ps.setInt(1, student.getNumber());
			ps.setInt(4, student.getAge());
			ps.setInt(5, student.getSex());
			ps.execute();
			System.out.println("新增學生成功");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
	/**
	 * 顯示學生資訊
	 */
	public void showAllStudentMessage(){
		String sql = "SELECT * FROM stu";
		Connection connect = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			rs = ps.executeQuery();
			
			System.out.println("姓名"+"\t"+"學號"+"\t"+"年齡"+"\t"+"年齡"+"\t"+"班級");
			while(rs.next()){
				String name = rs.getString("name");
				String gname = rs.getString("gname");
				int number = rs.getInt("number");
				int age = rs.getInt("age");
				int sex = rs.getInt("sex");
				System.out.println(name+"\t"+number+"\t"+age+"\t"+sex+"\t"+gname);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs != null)
					rs.close();
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
	
	/**
	 * 根據學生姓名檢視學生資訊
	 */
	public void findStudentMessageByName(){
		String sql = "SELECT * FROM stu WHERE name = ?";
		System.out.println("請輸入查詢的學生姓名:");
		String n = KeyBoardUtil.getStringKeyBoard();
		System.out.println("姓名"+"\t"+"學號"+"\t"+"年齡"+"\t"+"年齡"+"\t"+"班級");
		Connection connect = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			ps.setString(1, n);
			
			rs = ps.executeQuery();//執行sql查詢語句
			if(rs.next()){
				String name = rs.getString("name");
				String gname = rs.getString("gname");
				int number = rs.getInt("number");
				int age = rs.getInt("age");
				int sex = rs.getInt("sex");
				System.out.println(name+"\t"+number+"\t"+age+"\t"+sex+"\t"+gname);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
}

package cn.sxb.day11;

import java.io.ObjectInputStream.GetField;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.PreparedStatement;

public class GradeDemo extends UtilDB {

	public static void main(String[] args) {
		GradeDemo gb = new GradeDemo();
//		gb.createGradeTable();
//		gb.addGrade(gb.addGradebyKeyBoard());
//		gb.printMessageAllStudent();
//		gb.createView();
		gb.showGradeMessage();
	}

	/**
	 * 建立Grade表
	 */
	public void createGradeTable() {
		Connection connect = null;
		Statement statement = null;
		String sql = "create table grade(gradeNumber int(11) not null primary key,gradeName varchar(10) not null)";
		try {
			connect = getConnection();
			statement = connect.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (statement != null)
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		closeConnection(connect);
	}
	/**
	 * 鍵盤輸入班級名和編號
	 * @return 
	 */
	public Grade addGradebyKeyBoard(){
		System.out.println("請輸入班級名稱:");
		String gradeName = KeyBoardUtil.getStringKeyBoard();
		System.out.println("請輸入班級編號:");
		int gradeNumber = KeyBoardUtil.getintKeyBoard();
		Grade g = new Grade(gradeName,gradeNumber);
		return g;
	}
	/**
	 * 向grade表中新增班級
	 * @param g
	 */
	public void addGrade(Grade g){
		Connection connect = null;
		java.sql.PreparedStatement ps = null;
		String sql = "insert into grade values(?,?)";
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			ps.setString(2, g.gradeName);
			ps.setInt(1, g.gradeNumber);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps != null)
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		closeConnection(connect);
	}
	/**
	 * 顯示班級
	 */
	public void printMessageAllGrade(){
		String sql = "SELECT * FROM grade";
		Connection connect = null;
		java.sql.PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			rs = ps.executeQuery();
			
			System.out.println("班級名稱"+"\t"+"班級編號");
			while(rs.next()){
				int gradeNumber = rs.getInt("gradeNumber");
				String gradeName = rs.getString("gradeName");
				System.out.println(gradeName+"\t"+gradeNumber);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs != null)
					rs.close();
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
	
	/**
	 * 建立檢視
	 */
	public void createView(){
//		String sql = "create view s_g(gradeName,gradeNumber,name,number,age,sex)"
//				+ "as select gradeName,gradeNumber,name,number,age,sex "
//				+ "from grade,stu"
//				+ "where gradeName = gname";
		String sql = "create view vv as select * from grade,stu where gradeName = gname";
//		String ss = "select * from ss";
		Connection connect = null;
		java.sql.PreparedStatement ps = null;
		String s = "SELECT * FROM vv WHERE gradeName = ?";
		System.out.println("請輸入查詢的班級名稱:");
		String n = KeyBoardUtil.getStringKeyBoard();
		System.out.println("您要查詢的班級是:");
		System.out.println("班級"+"\t"+"編號"+"\t姓名"+"\t學號"+"\t年齡"+"\t性別");
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			ps.execute();
			
			ps = connect.prepareStatement(s);
			ps.setString(1, n);
			
			rs = ps.executeQuery();//執行sql查詢語句
			while(rs.next()){
				int gradeNumber = rs.getInt("gradeNumber");
				String gradeName = rs.getString("gradeName");
				String name = rs.getString("name");
				int number = rs.getInt("number");
				int age = rs.getInt("age");
				int sex = rs.getInt("sex");
				System.out.println(gradeName+"\t"+gradeNumber+"\t"+name+"\t"+number+"\t"+age+"\t"+sex);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		closeConnection(connect);	
	}
	/**
	 * 根據班級名稱查詢班級資訊
	 */
	public void showGradeMessage(){
		String sql = "SELECT * FROM grade,stu WHERE gradeName = ?";
		System.out.println("請輸入查詢的班級名稱:");
		String n = KeyBoardUtil.getStringKeyBoard();
		System.out.println("您要查詢的班級是:");
		System.out.println("班級"+"\t"+"編號"+"\t姓名"+"\t學號"+"\t年齡"+"\t性別");
		Connection connect = null;
		java.sql.PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			ps.setString(1, n);
			
			rs = ps.executeQuery();//執行sql查詢語句
			while(rs.next()){
				int gradeNumber = rs.getInt("gradeNumber");
				String gradeName = rs.getString("gradeName");
				String name = rs.getString("name");
				int number = rs.getInt("number");
				int age = rs.getInt("age");
				int sex = rs.getInt("sex");
				System.out.println(gradeName+"\t"+gradeNumber+"\t"+name+"\t"+number+"\t"+age+"\t"+sex);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
}

package cn.sxb.day11;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Test {

	public static void main(String[] args) {
		GradeDemo gb = new GradeDemo();
		StudentDemo sd = new StudentDemo();
		System.out.println("==歡迎進入班級管理系統==");
		for (;;) {
			System.out.println("==【班級管理系統主介面】==");
			System.out.println("1 班級管理, 2 學生管理,  3 退出");

			int num = 0;
			boolean a = true;
			System.out.println("<輸入操作命令>");
			while (a) {
				try {
					Scanner scanner = new Scanner(System.in);
					num = scanner.nextInt();
					a = false;
				} catch (InputMismatchException e) {
					String message = e.getMessage();
					System.out.println("輸入格式錯誤!\t請重新輸入正確格式");
					a = true;
				}
			}

			switch (num) {
			case 1:
				System.out.println("==班級管理==");
				System.out.println("1.建立班級, 2 顯示班級, 3 檢視班級學生, 4返回班級系統主介面");
				System.out.println("<輸入操作命令>");
				for (;;) {
					Scanner scanner = new Scanner(System.in);
					int numb = scanner.nextInt();
					if (numb == 4) {
						break;
					}
					switch (numb) {
					case 1:
						gb.addGrade(gb.addGradebyKeyBoard());
						break;
					case 2:
						gb.printMessageAllGrade();
						break;
					case 3:
						gb.createView();
						
					}
					System.out.println("<輸入操作命令>");
				}
				break;

			case 2:

				System.out.println("==學生管理==");
				System.out.println("新增學生, 2 顯示學生 , 3 查詢學生 , 4 返回班級系統主介面");
				for (;;) {
					Scanner scanner = new Scanner(System.in);
					int numbe = scanner.nextInt();
					if (numbe == 4) {
						break;
					}
					switch (numbe) {
					case 1:
						sd.addStudent(sd.addStudentMessageByKeyBoard());
						break;
					case 2:
						sd.showAllStudentMessage();
						break;
					case 3:
						sd.findStudentMessageByName();
					}
					System.out.println("<輸入操作命令>");
				}
				break;

			case 3:
				System.out.println("您已退出本系統");
				System.exit(0);
			}
		}
	}
}

對上面的系統進行分層:
package cn.GradeMangerSystem.bean;

public class Grade {
	private String gradeName;
	private int gradeNumber;//班級編號
	
	public Grade(String gradeName, int gradeNumber) {
		this.gradeName = gradeName;
		this.gradeNumber = gradeNumber;
	}

	public String getGradeName() {
		return gradeName;
	}

	public void setGradeName(String gradeName) {
		this.gradeName = gradeName;
	}

	public int getGradeNumber() {
		return gradeNumber;
	}

	public void setGradeNumber(int gradeNumber) {
		this.gradeNumber = gradeNumber;
	}
	
}

package cn.GradeMangerSystem.bean;

public class Student {
	private String name;
	private String gname;//班級編號
	private int number;
	private int age;
	private int sex;
	
	public Student(String name, String gname, int number, int age, int sex) {
		this.name = name;
		this.gname = gname;
		this.number = number;
		this.age = age;
		this.sex = sex;
	}
	
	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getGname() {
		return gname;
	}


	public void setGname(String gname) {
		this.gname = gname;
	}


	public int getNumber() {
		return number;
	}


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


	public int getAge() {
		return age;
	}


	public void setAge(int age) {
		this.age = age;
	}


	public int getSex() {
		return sex;
	}


	public void setSex(int sex) {
		this.sex = sex;
	}

}

package cn.GradeMangerSystem.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class KeyBoardUtil {
	/**
	 * 使用io流,獲取從鍵盤輸入到程式。使用Input(從外部裝置到程式為輸入,程式到外部裝置為輸出);
	 * System.in(out)標準系統輸入輸出中,不需要關閉流,由系統自動關閉。否則在後面測試類中會回報流關閉錯誤
	 */
	public static String getStringKeyBoard() {
		String name = "";
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			isr = new InputStreamReader(System.in);
			br = new BufferedReader(isr);
			name = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return name;
	}

	public static int getintKeyBoard() {
		while (true) {
			int number = 0;
			InputStreamReader isr = null;
			BufferedReader br = null;
			try {
				isr = new InputStreamReader(System.in);
				br = new BufferedReader(isr);
				String s = br.readLine();// 在緩衝流中用readLine(),每行讀成String型。在轉換成int型
				number = Integer.parseInt(s);

				return number;

			} catch (IOException e) {
				e.printStackTrace();
			} catch (NumberFormatException a) {
				System.out.println("請輸入正確數值格式");
			}
		}
	}
}

package cn.GradeMangerSystem.contrl;

import cn.GradeMangerSystem.bean.Grade;
import cn.GradeMangerSystem.utils.KeyBoardUtil;

public class GradeContrl {
	/**
	 * 單例模式
	 */
	private static GradeContrl gc = null;
	public static GradeContrl getInstance(){
		if(gc == null){
			gc = new GradeContrl(); 
		}
		return gc;
	}
	private GradeContrl(){
	}
	
	/**
	 * 鍵盤錄入班級資訊
	 */
	public Grade addGradebyKeyBoard(){
		System.out.println("請輸入班級名稱:");
		String gradeName = KeyBoardUtil.getStringKeyBoard();
		System.out.println("請輸入班級編號:");
		int gradeNumber = KeyBoardUtil.getintKeyBoard();
		Grade g = new Grade(gradeName,gradeNumber);
		return g;
	}
}

package cn.GradeMangerSystem.contrl;

import cn.GradeMangerSystem.bean.Student;
import cn.GradeMangerSystem.utils.KeyBoardUtil;

public class StudentContrl {
	/**
	 * 單例模式
	 */
	private static StudentContrl sc = null;
	public static StudentContrl getInstance(){
		if(sc == null){
			sc = new StudentContrl(); 
		}
		return sc;
	}
	private StudentContrl(){}
	/**
	 * 鍵盤錄入學生資訊
	 */
	public Student addStudentMessageByKeyBoard(){
		System.out.println("請輸入學生姓名:");
		String name = KeyBoardUtil.getStringKeyBoard();
		System.out.println("請輸入學生班級:");
		String gname = KeyBoardUtil.getStringKeyBoard();
		System.out.println("請輸入學生學號:");
		int number = KeyBoardUtil.getintKeyBoard();
		System.out.println("請輸入學生年齡:");
		int age = KeyBoardUtil.getintKeyBoard();
		System.out.println("請輸入學生性別:");
		int sex = KeyBoardUtil.getintKeyBoard();
		
		Student student = new Student(name,gname,number,age,sex);
		return student;
	}
}

package cn.GradeMangerSystem.model;

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

public class ConnectDB {
	private static final String URL = "jdbc:mysql://192.168.5.7:3306/db1606";
	private static final String USER = "test";
	private static final String PASSWORD = "123321";

	/**
	 * 構造方法載入驅動
	 */
	public ConnectDB() {
		try {
			Class.forName("com.mysql.jdbc.Driver");// 載入mysql驅動
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 建立連線資料庫
	 */
	public Connection getConnection() {
		Connection connect = null;
		try {
			connect = DriverManager.getConnection(URL, USER, PASSWORD);// 試圖建立到給定資料庫URL的連線
//			if (connect == null) {
//				System.out.println("資料庫連線失敗");
//			} else {
//				System.out.println("資料庫連線成功");
//			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connect;
	}

	/**
	 * 關閉資料庫連線
	 */
	public void closeConnection(Connection connect) {
		try {
			if (connect != null)
				connect.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
package cn.GradeMangerSystem.model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.GradeMangerSystem.bean.Grade;
import cn.GradeMangerSystem.utils.KeyBoardUtil;

public class GradeDemo extends ConnectDB {

	public static void main(String[] args) {
//		GradeDemo gb = new GradeDemo();
//		gb.createGradeTable();
//		gb.addGrade(gb.addGradebyKeyBoard());
//		gb.printMessageAllStudent();
//		gb.createView();
//		gb.showGradeMessage();
	}
	/**
	 * 單例模式
	 */
	private static GradeDemo gb = null;
	public static GradeDemo getInstance(){
		if(gb == null){
			gb = new GradeDemo();
		}
		return gb;
	}
	private GradeDemo(){}
	/**
	 * 建立Grade表
	 */
	public void createGradeTable() {
		Connection connect = null;
		Statement statement = null;
		/**
		 * 建立表時,將gradeNumber設定主鍵,是避免gradeNumber重複(重複會報錯)
		 */
		String sql = "create table grade(gradeNumber int(11) not null primary key,gradeName varchar(10) not null)";
		try {
			connect = getConnection();
			statement = connect.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (statement != null)
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		closeConnection(connect);
	}
	
	/**
	 * 向grade表中新增班級
	 * @param g
	 */
	public void addGrade(Grade g){
		Connection connect = null;
		java.sql.PreparedStatement ps = null;
		String sql = "insert into grade values(?,?)";
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			ps.setString(2, g.getGradeName());
			ps.setInt(1, g.getGradeNumber());
			/**
			 * 在這裡原來g.gradeName和g.gradeNumber是呼叫構造方法的屬性,所以在用mvc模式分層時,
			 * 會報錯(需將班級屬性改為public).而mvc的規則之一是:每個屬性對應有get()和set()方法
			 */
//			ps.setString(2, g.gradeName);
//			ps.setInt(1, g.gradeNumber);
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ps != null)
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		closeConnection(connect);
	}
	/**
	 * 顯示班級
	 */
	public void printMessageAllGrade(){
		String sql = "SELECT * FROM grade";
		Connection connect = null;
		java.sql.PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			rs = ps.executeQuery();
			
			System.out.println("班級名稱"+"\t"+"班級編號");
			while(rs.next()){
				int gradeNumber = rs.getInt("gradeNumber");
				String gradeName = rs.getString("gradeName");
				System.out.println(gradeName+"\t"+gradeNumber);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs != null)
					rs.close();
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
	
	/**
	 * 建立檢視,在檢視中檢視
	 
	public void createView(){
//		String sql = "create view s_g(gradeName,gradeNumber,name,number,age,sex)"
//				+ "as select gradeName,gradeNumber,name,number,age,sex "
//				+ "from grade,stu"
//				+ "where gradeName = gname";
		String sql = "create view vv as select * from grade,stu where gradeName = gname";
//		String ss = "select * from ss";
		Connection connect = null;
		java.sql.PreparedStatement ps = null;
		String s = "SELECT * FROM vv WHERE gradeName = ?";
		System.out.println("請輸入查詢的班級名稱:");
		String n = KeyBoardUtil.getStringKeyBoard();
		System.out.println("您要查詢的班級是:");
		System.out.println("班級"+"\t"+"編號"+"\t姓名"+"\t學號"+"\t年齡"+"\t性別");
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			ps.execute();
			
			ps = connect.prepareStatement(s);
			ps.setString(1, n);
			
			rs = ps.executeQuery();//執行sql查詢語句
			while(rs.next()){
				int gradeNumber = rs.getInt("gradeNumber");
				String gradeName = rs.getString("gradeName");
				String name = rs.getString("name");
				int number = rs.getInt("number");
				int age = rs.getInt("age");
				int sex = rs.getInt("sex");
				System.out.println(gradeName+"\t"+gradeNumber+"\t"+name+"\t"+number+"\t"+age+"\t"+sex);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if (ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		closeConnection(connect);	
	}*/
	/**
	 * 根據班級名稱查詢班級資訊
	 */
	public void showGradeMessage(){
		String sql = "SELECT * FROM grade,stu WHERE gradeName = ?";
		System.out.println("請輸入查詢的班級名稱:");
		String n = KeyBoardUtil.getStringKeyBoard();
		System.out.println("您要查詢的班級是:");
		System.out.println("班級"+"\t"+"編號"+"\t姓名"+"\t學號"+"\t年齡"+"\t性別");
		Connection connect = null;
		java.sql.PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			ps.setString(1, n);
			
			rs = ps.executeQuery();//執行sql查詢語句
			while(rs.next()){
				int gradeNumber = rs.getInt("gradeNumber");
				String gradeName = rs.getString("gradeName");
				String name = rs.getString("name");
				int number = rs.getInt("number");
				int age = rs.getInt("age");
				int sex = rs.getInt("sex");
				System.out.println(gradeName+"\t"+gradeNumber+"\t"+name+"\t"+number+"\t"+age+"\t"+sex);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
	
}

package cn.GradeMangerSystem.model;

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

import cn.GradeMangerSystem.bean.Student;
import cn.GradeMangerSystem.utils.KeyBoardUtil;

public class StudentDemo extends ConnectDB{

	public static void main(String[] args) {
		StudentDemo sd = new StudentDemo();
//		sd.createGradeTable();
//		sd.addStudent(sd.addStudentMessageByKeyBoard());
//		sd.showAllStudentMessage();
//		sd.findStudentMessageByName();
	}
	/**
	 * 單例模式
	 */
	private static StudentDemo sd = null;
	public static StudentDemo getInstance(){
		if(sd == null){
			sd = new StudentDemo();
		}
		return sd;
	}
	private StudentDemo(){}
	
	/**
	 * 建立stu表
	 */
	public void createGradeTable() {
		Connection connect = null;
		Statement statement = null;
		String sql = "create table  stu (" + "number int(11) not null primary key," // 主鍵
				+ "name varchar(10) not null," // 姓名
				+ "gname varchar(10)  not null," // 班級名
				+ "age int(3) not null," // 年齡
				+ "sex int(1) not null" // 性別
				+ ")";
		try {
			connect = getConnection();
			statement = connect.createStatement();
			statement.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (statement != null)
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		closeConnection(connect);
	}

	/**
	 * 向stu表中新增學生
	 */
	public void addStudent(Student student){
		String sql = "insert into stu values(?,?,?,?,?)";
		Connection connect = null;
		PreparedStatement ps = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);

			ps.setString(2, student.getName());
			ps.setString(3, student.getGname());
			ps.setInt(1, student.getNumber());
			ps.setInt(4, student.getAge());
			ps.setInt(5, student.getSex());
			ps.execute();
			System.out.println("新增學生成功");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
	/**
	 * 顯示學生資訊
	 */
	public void showAllStudentMessage(){
		String sql = "SELECT * FROM stu";
		Connection connect = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			rs = ps.executeQuery();
			
			System.out.println("姓名"+"\t"+"學號"+"\t"+"年齡"+"\t"+"年齡"+"\t"+"班級");
			while(rs.next()){
				String name = rs.getString("name");
				String gname = rs.getString("gname");
				int number = rs.getInt("number");
				int age = rs.getInt("age");
				int sex = rs.getInt("sex");
				System.out.println(name+"\t"+number+"\t"+age+"\t"+sex+"\t"+gname);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs != null)
					rs.close();
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
	
	/**
	 * 根據學生姓名檢視學生資訊
	 */
	public void findStudentMessageByName(){
		String sql = "SELECT * FROM stu WHERE name = ?";
		System.out.println("請輸入查詢的學生姓名:");
		String n = KeyBoardUtil.getStringKeyBoard();
		System.out.println("姓名"+"\t"+"學號"+"\t"+"年齡"+"\t"+"年齡"+"\t"+"班級");
		Connection connect = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connect = getConnection();
			ps = connect.prepareStatement(sql);
			ps.setString(1, n);
			
			rs = ps.executeQuery();//執行sql查詢語句
			if(rs.next()){
				String name = rs.getString("name");
				String gname = rs.getString("gname");
				int number = rs.getInt("number");
				int age = rs.getInt("age");
				int sex = rs.getInt("sex");
				System.out.println(name+"\t"+number+"\t"+age+"\t"+sex+"\t"+gname);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			closeConnection(connect);
		}
	}
}

package cn.GradeMangerSystem.view;

import java.util.InputMismatchException;
import java.util.Scanner;

import cn.GradeMangerSystem.contrl.GradeContrl;
import cn.GradeMangerSystem.contrl.StudentContrl;
import cn.GradeMangerSystem.model.GradeDemo;
import cn.GradeMangerSystem.model.StudentDemo;

public class Test {

	public static void main(String[] args) {
		System.out.println("==歡迎進入班級管理系統==");
		
		for (;;) {
			System.out.println("==【班級管理系統主介面】==");
			System.out.println("1 班級管理, 2 學生管理,  3 退出");
			int num = 0;
			boolean a = true;
			System.out.println("<輸入操作命令>");
			while (a) {
				try {
					Scanner scanner = new Scanner(System.in);
					num = scanner.nextInt();
					a = false;
				} catch (InputMismatchException e) {
					String message = e.getMessage();
					System.out.println("輸入格式錯誤!\n請重新輸入正確格式:");
					a = true;
				}
			}

			switch (num) {
			case 1:
				System.out.println("==班級管理==");
				System.out.println("1.建立班級, 2 顯示班級, 3 檢視班級學生, 4返回班級系統主介面");
				System.out.println("<輸入操作命令>");
				for (;;) {
					Scanner scanner = new Scanner(System.in);
					int numb = scanner.nextInt();
					if (numb == 4) {
						break;
					}
					switch (numb) {
					case 1:
						GradeDemo.getInstance().addGrade(GradeContrl.getInstance().addGradebyKeyBoard());
						break;
					case 2:
						GradeDemo.getInstance().printMessageAllGrade();
						break;
					case 3:
						GradeDemo.getInstance().showGradeMessage();
					}
					System.out.println("<輸入操作命令>");
				}
				break;

			case 2:
				System.out.println("==學生管理==");
				System.out.println("1.新增學生, 2 顯示學生 , 3 查詢學生 , 4 返回班級系統主介面");
				for (;;) {
					Scanner scanner = new Scanner(System.in);
					int numbe = scanner.nextInt();
					if (numbe == 4) {
						break;
					}
					switch (numbe) {
					case 1:
						StudentDemo.getInstance().addStudent(StudentContrl.getInstance().addStudentMessageByKeyBoard());
						break;
					case 2:
						StudentDemo.getInstance().showAllStudentMessage();
						break;
					case 3:
						StudentDemo.getInstance().findStudentMessageByName();
					}
					System.out.println("<輸入操作命令>");
				}
				break;
				
			case 3:
				System.out.println("您已退出本系統");
				System.exit(0);
			}
		}
	}
}