【从零开始学Java笔记】学生管理系统

大家可以关注作者的账号,关注从零开始学Java笔记文集。也可以根据目录前往作者的博客园博客进行学习。本片文件将基于黑马程序员就业班视频进行学习以及资料的分享,并记录笔记和自己的看法。欢迎大家一起学习和讨论。

【从零开始学Java笔记】目录

学生管理系统一直是大学学习过程中无法跳过的环节,因为这个系统囊括了很多知识,是初学者不错的上手项目。

需求:
1.学生属性有学号,姓名,年龄和地址
2.要有主界面
3.可以增加学生,学号唯一,不能重复添加
4.可以查看所有学生
5.可以根据学号删除学生
6.可以根据学号查找学生

实现:

package com.tyust.studentmanagement;

public class Student {
	
	//成员变量
	private String id;
	private String name;
	private String age;
	private String address;
		
	
	//get、set方法
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

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

	public String getAge() {
		return age;
	}

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

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	//无参构造
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	//有参构造
	public Student(String id, String name, String age, String address) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
}
package com.tyust.studentmanagement;

import java.util.ArrayList;
import java.util.Scanner;

/*
 * 
 */
public class StudentAction {
	public static void main(String[] args) {

		// 创建一个集合
		ArrayList<Student> stu = new ArrayList<Student>();

		// 主界面,while(true)是一个死循环
		while (true) {
			System.out.println("**********欢迎来到学生管理系统**********");
			System.out.println("如需添加学生信息,请按1");
			System.out.println("如需查看学生信息,请按2");
			System.out.println("如需删除学生信息,请按3");
			System.out.println("如需修改学生信息,请按4");
			System.out.println("如需退出,请按5");
			System.out.println("请输入您需要的操作:");

			// 从键盘输入
			Scanner sc = new Scanner(System.in);
			// 接收
			int choice = sc.nextInt();

			// switch选择
			switch (choice) {
			case 1:
				addStudent(stu);
				break;
			case 2:
				travelStudent(stu);
				break;
			case 3:
				deleteStudent(stu);
				break;
			case 4:
				modifyStudent(stu);
				break;
			// 这个地方用了case穿透,即case 5没有内容,直接穿透到下一个case
			case 5:
			default:
				System.out.println("谢谢您的使用!");
				// 关闭虚拟机,结束程序
				System.exit(0);

			}
		}
	}

	// 增加学生
	public static void addStudent(ArrayList<Student> stu) {

		// 键盘输入
		Scanner sc = new Scanner(System.in);
		// 提拉,将id的声明提拉至循环外,以便循环后还能使用,如果在循环内声明,循环结束后,变量也随之消失
		String id;

		// while循环,判断学号是否重复
		while (true) {
			System.out.println("请输入学号:");
			id = sc.nextLine();
			// 判断标志初始化
			boolean index = false;
			// 循环判断,如果发现相同,改变判断标志,跳出循环
			for (int i = 0; i < stu.size(); i++) {
				if (id.equals(stu.get(i).getId())) {
					index = true;
					break;
				}
			}

			// 判断
			if (index) {
				System.out.println("您输入的学号已存在!请重新输入。");
				System.out.println("如需重新输入学生信息,请按1");
				System.out.println("如需回主菜单,请按2");
				System.out.println("请输入您需要的操作:");
				Scanner sc1 = new Scanner(System.in);
				// 接收
				int flag = sc1.nextInt();

				// switch选择
				switch (flag) {
				case 1:
					// 跳出判断继续循环
					break;

				case 2:
				default:
					// return直接结束方法
					return;
				}
			} else
				break;
		}

		// 输入其他信息,并添加至集合
		System.out.println("请输入姓名:");
		String name = sc.nextLine();
		System.out.println("请输入年龄:");
		String age = sc.nextLine();
		System.out.println("请输入地址:");
		String address = sc.nextLine();
		Student s = new Student(id, name, age, address);

		stu.add(s);

		System.out.println("添加成功");

	}

//查看所有叙述
	public static void travelStudent(ArrayList<Student> stu) {
		//如果没有学生显示没有学生,如果有显示学生信息
		if (stu.size() == 0) {
			System.out.println("没有学生");
		} else {
			System.out.println("学号\t\t姓名\t年龄\t地址\t");
			for (int i = 0; i < stu.size(); i++) {
				Student s = stu.get(i);
				System.out.println(s.getId() + "\t\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
			}
		}

	}
//修改学生信息
	public static void modifyStudent(ArrayList<Student> stu) {
		System.out.println("请输入要修改学生的学号:");
		//接收学号
		Scanner sc = new Scanner(System.in);
		String id = sc.nextLine();
		//标志初始化
		boolean judge = false;
		int index = -1;
		//遍历寻找学生,找到修改标志
		for (int i = 0; i < stu.size(); i++) {
			if (id.equals(stu.get(i).getId())) {
				
				judge = true;
				index = i;
				break;
			}
		}

		if (judge) {

			
			System.out.println("请重新输入姓名:");
			String name = sc.nextLine();
			System.out.println("请重新输入年龄:");
			String age = sc.nextLine();
			System.out.println("请重新输入地址:");
			String address = sc.nextLine();
			//新建一个对象
			Student s = new Student(id, name, age, address);
			//修改
			stu.set(index,s);

			System.out.println("修改成功");
		} else {
			System.out.println("没有这个学生!");
		}
	}
//删除学生信息
	public static void deleteStudent(ArrayList<Student> stu) {
		System.out.println("请输入要删除学生的学号:");
		//接收学生学号
		Scanner sc = new Scanner(System.in);
		String id = sc.nextLine();
		//标注初始化
		boolean judge = false;
		//遍历寻找
		for (int i = 0; i < stu.size(); i++) {
			if (id.equals(stu.get(i).getId())) {
				//移除
				stu.remove(i);

				judge = true;
				break;
			}
		}
		//判断
		if (judge) {
			System.out.println("删除成功!");
		} else {
			System.out.println("没有这个学生!");
		}
	}

}

package cn.com.dao.chivementdao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import cn.com.util.DBConnection; import cn.com.util.DBSql; import cn.com.vo.chivementvo.ChivementVo; public class ExamDao { private Connection conn = DBConnection.getConnectionOracle(); private ChivementVo examVo; public ExamDao() { } public ExamDao(ChivementVo examVo) { super(); this.examVo = examVo; } /** * 全部查询 */ public Object[][] selectAll() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_ALL); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberAll(DBSql.SELECT_ALL_COUNT)][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据号查询 */ public Object[][] selectBySid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_S_ID); ps.setInt(1, examVo.getS_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_S_ID_COUNT, examVo .getS_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据组号查询 */ public Object[][] selectByGid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_G_ID); ps.setInt(1, examVo.getG_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_G_ID_COUNT, examVo .getG_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据课程号查询 */ public Object[][] selectByCid() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_C_ID); ps.setInt(1, examVo.getC_id()); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumber(DBSql.SELECT_BY_C_ID_COUNT, examVo .getC_id())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { // System.out.println( examVo.getG_id()); date[i][j] = rs.getObject(j+1); } i++; } // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据姓名模糊查询 * * @return */ public Object[][] selectByName() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_S_NAME); ps.setString(1, examVo.getS_name()); ps.setString(2, "%" + examVo.getS_name() + "%"); ps.setString(3, "%" + examVo.getS_name()); ps.setString(4, examVo.getS_name() + "%"); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberByName(DBSql.SELECT_BY_S_NAME_COUNT, examVo.getS_name())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 根据课程名称模糊查询 * * @return */ public Object[][] selectByClassName() { Object date[][] = null; int max = 0; int i = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.SELECT_BY_CLASS_NAME); ps.setString(1, examVo.getClass_name()); ps.setString(2, "%" + examVo.getClass_name() + "%"); ps.setString(3, "%" + examVo.getClass_name()); ps.setString(4, examVo.getClass_name() + "%"); rs = ps.executeQuery(); // 得到列数 max = rs.getMetaData().getColumnCount(); date = new Object[getnumberByName(DBSql.SELECT_BY_CLASS_COUNT, examVo.getClass_name())][max]; while (rs.next()) { for (int j = 0; j < max; j++) { date[i][j] = rs.getObject(j + 1); } i++; } } catch (SQLException e) { e.printStackTrace(); } return date; } /** * 修改选中生的成绩 * */ public void updatSelectClass() { PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(DBSql.UPDATE_EXAM_BY_STUID); ps.setInt(1, examVo.getClassExamChivement()); ps.setInt(2, examVo.getS_id()); ps.setInt(3, examVo.getC_id()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } /** * 得到所有课程号和课程名 * * @return */ public String[] getClassNoName() { String[] classNoName = null; PreparedStatement ps = null; ResultSet rs = null; int j = 0; try { int i = getnumberAll(DBSql.SELECT_CLASS_NAME_COUNT); classNoName = new String[i + i]; ps = conn.prepareStatement(DBSql.SELECT_CLASS_NAME); rs = ps.executeQuery(); while (rs.next()) { classNoName[j] = rs.getString(1); classNoName[j + i] = rs.getString(2); j++; } } catch (SQLException e) { e.printStackTrace(); } return classNoName; } /** * 根据科目修改成绩 查询号 姓名 成绩 * */ public void SelectClassStuName() { int i = 0; PreparedStatement ps = null; ResultSet rs = null; int j = getnumberBySelectClassName( DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM_COUNT, examVo.getC_id()); int[] sNum = new int[j]; String[] sName = new String[j]; int[] classExam = new int[j]; try { ps = conn.prepareStatement(DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM); ps.setInt(1, examVo.getC_id()); rs = ps.executeQuery(); while (rs.next()) { sNum[i] = rs.getInt(1); sName[i] = rs.getString(2); classExam[i] = rs.getInt(3); i++; } } catch (SQLException e) { e.printStackTrace(); } examVo.setSid(sNum); examVo.setSname(sName); examVo.setClassExam(classExam); } /** * 根据科目修改成绩 查询号 姓名 成绩 后修改成绩 * */ public void UpdateClassStuName() { // int i = 0; PreparedStatement ps = null; ResultSet rs = null; int j = getnumberBySelectClassName( DBSql.SELECT_CLASS_STU_SNO_SNAME_EXAM_COUNT, examVo.getC_id()); try { ps = conn.prepareStatement(DBSql.UPDATE_CHIVEMENT_BY_CLASS); for(int i =0;i<j;i++){ ps.setInt(1, examVo.getClassExam()[i]); ps.setInt(2, examVo.getSid()[i]); ps.setInt(3, examVo.getC_id()); ps.executeUpdate(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 全部查询获得行数 * * @return */ public int getnumberAll(String str) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return number; } /** * 根据号 根据组号 根据课程号查询 获得行数 * * @return */ public int getnumber(String str, int i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setInt(1, i); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); // rs.close(); // ps.close(); // conn.close(); } catch (SQLException e) { e.printStackTrace(); } return number; } /** * 根据姓名 课程名 查询 获得行数 * */ public int getnumberByName(String str, String i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setString(1, i); ps.setString(2, "%" + i + "%"); ps.setString(3, "%" + i); ps.setString(4, i + "%"); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); } return number; } // /** // * 根据课程名查询 获得行数 // * // */ // // public int getnumberByClassName(String str, String i) { // int number = 0; // PreparedStatement ps = null; // ResultSet rs = null; // try { // ps = conn.prepareStatement(str); // ps.setString(1, i); // ps.setString(2, "%" + i + "%"); // ps.setString(3, "%" + i); // ps.setString(4, i + "%"); // rs = ps.executeQuery(); // rs.next(); // number = rs.getInt(1); // } catch (SQLException e) { // e.printStackTrace(); // } // return number; // } /** * * 根据课程名修改成绩获得行数 * * @param str * @param i * @return */ public int getnumberBySelectClassName(String str, int i) { int number = 0; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(str); ps.setInt(1, i); rs = ps.executeQuery(); rs.next(); number = rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); } return number; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值