Student

在这里插入图片描述

java学生成绩管理系统项目,数据库,面向对象,分层

如果你觉得有点用,请留下你的点赞

项目主要功能界面展示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z03WCxUV-1629341968113)(D:\mycsdn\images\image-20210819104827398.png)]

实体类:

1、学生实体类:

package com.stu.entity;


public class Student {

	private String stuNo;
	private String stuName;

	// 三门课成绩
	private double[] scores = new double[3];

	private double avg;
	private double sum;
	
	
	private int stuIndex =-1;//当前学员所在数组的索引

	public int getStuIndex() {
		return stuIndex;
	}

	public void setStuIndex(int stuIndex) {
		this.stuIndex = stuIndex;
	}

	public String getStuNo() {
		return stuNo;
	}

	public void setStuNo(String stuNo) {
		this.stuNo = stuNo;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public double[] getScores() {
		return scores;
	}

	public void setScores(double[] scores) {
		this.scores = scores;
	}

	public double getAvg() {
		return avg;
	}

	/*public void setAvg(double avg) {
		this.avg = avg;
	}*/

	public double getSum() {
		return sum;
	}

	public void setSum(double sum) {
		this.sum = sum;
		this.avg = this.sum / 3;
	}

	public Student(String stuNo, String stuName, double[] scores, double avg, double sum) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.scores = scores;
		this.avg = avg;
		this.sum = sum;
	}

	public Student() {
		super();
	}

	@Override
	public String toString() {
		return this.stuNo + "\t" + this.stuName + "\t" + this.scores[0] + "\t" + this.scores[1] + "\t" + this.scores[2]
				+ "\t" + String.format("%.2f", this.avg) + "\t" + String.format("%.2f", this.sum);
	}

}

2、用户信息实体类

package com.stu.entity;

public class UserInfo {
	private String userName;
	private String userPwd;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public UserInfo(String userName, String userPwd) {
		super();
		this.userName = userName;
		this.userPwd = userPwd;
	}

	public UserInfo() {
		super();
	}
	

	@Override
	public String toString() {
		return "UserInfo [userName=" + userName + ", userPwd=" + userPwd + "]";
	}

}

Dao层(持久层)

1、BaseDao

package com.student.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.student.dao.IGetEntity;
import com.student.db.ConnectionDB;
import com.student.entity.Student;

public class BaseDao<T> {
	
	//查询的方法
	public ArrayList<T> getAllInfo(String sql,IGetEntity<T> getEntity,Object...param){
		ArrayList<T> list = new ArrayList<>();
		//链接数据库
		Connection coon = ConnectionDB.getCoon();
		PreparedStatement pstmt = null;
		ResultSet res = null;
		
		 try {
			 //获取发送sql的对象
			pstmt = coon.prepareStatement(sql);
			//传参
			for(int i = 0;i<param.length;i++) {
				pstmt.setObject(i+1, param[i]);
			}
			
			//执行sql 
		 res = pstmt.executeQuery();
		 while(res.next()) {
			 T t = getEntity.getEntity(res);
			 list.add(t);
		 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			ConnectionDB.closeDB(coon, pstmt, res);
		}
		
		return list;
	}

	//增删改方法
	public boolean executeSQL(String sql ,Object...param){
		//链接数据库
		Connection coon = ConnectionDB.getCoon();
		//获取发送sql需要的容器
		PreparedStatement pstmt = null;
		
		try {
			//发送SQL
			pstmt = coon.prepareStatement(sql);
			//设置参数
			for(int i = 0;i<param.length;i++) {
				pstmt.setObject(i+1, param[i]);
			}
			//执行sql
			int row = pstmt.executeUpdate();
			if (row>0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			ConnectionDB.closeDB(coon, pstmt, null);
		}
		
		return false;
		
	}
	
	//获取一个实体
	public T getOneEntity(String sql, IGetEntity<T> getEntity, Object... param) {

		Connection conn = ConnectionDB.getCoon();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		T t = null;
		try {

			// 3.发送sql语句,执行sql语句,返回结果
			pstmt = conn.prepareStatement(sql); // sql参数从这里传入
			// 执行

			// 设置参数的值
			// pstmt.setString(1, stuNo);
			for (int i = 0; i < param.length; i++) {
				pstmt.setObject(i + 1, param[i]);
			}

			rs = pstmt.executeQuery();
			if (rs.next()) {
				t = getEntity.getEntity(rs);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally { // 不管是否有发生异常,都会执行
			ConnectionDB.closeDB(conn, pstmt, rs);
		}

		return t;
	}
}

2、IGetEntity接口

package com.student.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.student.entity.Student;

public interface IGetEntity<T> {
	T getEntity(ResultSet res) throws SQLException;
}

3、StudentDao(操作学生的功能)

package com.student.dao;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.student.db.ConnectionDB;
import com.student.entity.Student;


public class StudentDao<T> extends BaseDao implements IGetEntity<Student>{

	
	//显示学生
	public  ArrayList<Student> showAll() {
		//执行的sql语句
		String sql = "select * from students";
		return this.getAllInfo(sql,this);
		
	}
	
	
	
	//更新信息
	public boolean updateSutdent(Student s) {
		
		String sql = "update students set name = ? ,sc1=?,sc2=?,sc3=?,avg=?,sum=? where stuId = ?";
		Object[] param = {s.getName(),s.getSc1(),s.getSc2(),s.getSc3(),s.getAvg(),s.getSum(),s.getStuId()};
		return this.executeSQL(sql, param);
	}
	
	//添加学生
	public boolean addStu(Student s) {
		
		String sql = "insert into students (stuId,name,sc1,sc2,sc3,avg,sum) VALUES(?,?,?,?,?,?,?);";
		Object[] param = {s.getStuId(),s.getName(),s.getSc1(),s.getSc2(),s.getSc3(),s.getAvg(),s.getSum()};
		return this.executeSQL(sql, param);
	}
	
	
	
	//学号查找一个学生
	public Student serchStu(String id) {
		//sql语句
		String sql = "select * from students where stuId = ?";
		return   (Student) this.getOneEntity(sql, this, id);
	}
	
	//更新学生信息
	public boolean updateStu(Student s) {
		//sql语句
		String sql = "update students set name = ? ,sc1 = ?,sc2=?,sc3=?,avg=?,sum=? where stuId = ?";
		Object[] param = {s.getName(),s.getSc1(),s.getSc2(),s.getSc3(),s.getAvg(),s.getSum(),s.getStuId()};
		
		return this.executeSQL(sql, param);
	}
	
	//根据学号删除
    public boolean deleteStu(String num){
    	//sql语句
    	String sql = "delete from students where  stuId = ?";
    	return this.executeSQL(sql, num);
	}
    	
    	
    
    //根据姓名模糊查找
	public ArrayList<Student> serchByname(String keyName) {
		//sql语句
		String sql = "select * from students where name like ?";
		keyName = "%"+keyName+"%";
		return this.getAllInfo(sql, this, keyName);
	}

	//总分排序

	public ArrayList<Student> sortBySum() {
		//sql语句
		String sql = "select * from students order by sum desc;";
		return this.getAllInfo(sql, this);
	}

	public ArrayList<Student> sortById() {
		//sql语句
				String sql = "select * from students order by stuId asc;";
				return this.getAllInfo(sql, this);
	}

	//序列化
	public boolean writeStu(ArrayList<Student> stuList) {
		
		//创建文件对象
		File file = new File("src/student.txt");
		//序列化对象
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		try {
			fos = new FileOutputStream(file);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(stuList);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return false;
		}finally {
			try {
				oos.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		 
		return true;
	}

	@Override
	public Student getEntity(ResultSet res) throws SQLException {
		Student stu = new Student();
		stu.setStuId(res.getString("stuId"));
		stu.setName(res.getString("name"));
		stu.setSc1(res.getInt("sc1"));
		stu.setSc2(res.getInt("sc2"));
		stu.setSc3(res.getInt("sc3"));
		
		stu.setSum();
		stu.setAvg();
		
		return stu;
	}
    
    


	
}

4、实现用户注册的Dao

package com.student.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.student.db.ConnectionDB;
import com.student.entity.StudentInfo;
import sun.security.pkcs11.P11TlsKeyMaterialGenerator;

public class StudentInfoDao {

	// 注册新用户
	public boolean addStudent(StudentInfo user) {
		// 链接数据库
		Connection coon = ConnectionDB.getCoon();
		PreparedStatement ptmt = null;
		// 编写sql语句
		String sql = "insert into userinfo (username,password) VALUES(?,?)";
		try {
			// 获取发送sql的对象
			ptmt = coon.prepareStatement(sql);
			// 传参
			ptmt.setString(1, user.getUserName());
			ptmt.setString(2, user.getPassWord());
			// 执行sql
			int executeUpdate = ptmt.executeUpdate();
			if (executeUpdate==1) {
				return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				// 关闭链接
				ptmt.close();
				coon.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return false;
	}

	// 修改密码
	public boolean modStudent(StudentInfo studentInfo) {
		PreparedStatement ptmt = null;
		// 获取链接
		Connection coon = ConnectionDB.getCoon();
		// sql 语句
		String sql = "update userinfo set password = ? where username = ?";
		try {
			// 获取发送sql语句的对象
			ptmt = coon.prepareStatement(sql);
			// 传参
			ptmt.setString(1, studentInfo.getPassWord());
			ptmt.setString(2, studentInfo.getUserName());
			// 执行sql
			int flag = ptmt.executeUpdate();
			if (flag == 1) {
				return true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ptmt.close();
				coon.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

	// 注销用户
	public boolean delStudent(StudentInfo studentInfo) {

		return false;
	}

	// 查看所有
	public ArrayList<StudentInfo> getAll() {
		ArrayList<StudentInfo> arrayList = new ArrayList<>();
		return arrayList;
	}

	// 查询密码
	public StudentInfo serch(String name) {

		return null;
	}

	// 验证登录
	public StudentInfo login(String username, String password) {

		// 查询语句
		String sql = "select * from userinfo where username = ? and password = ?";
		// 链接数据库
		Connection coon = ConnectionDB.getCoon();
		PreparedStatement ptmt = null;
		ResultSet res = null;
		StudentInfo user = null;
		try {
			// 获取发送sql需要的对象
			ptmt = coon.prepareStatement(sql);
			// 传参
			ptmt.setString(1, username);
			ptmt.setString(2, password);
			// 执行sql
			res = ptmt.executeQuery();
			// 判断是否正确
			if (res.next()) {
				user = new StudentInfo();
				String uname = res.getString("username");
				String pwd = res.getString("password");
				user.setUserName(uname);
				user.setPassWord(pwd);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionDB.closeDB(coon, ptmt, res);
		}

		return user;
	}

	public boolean getUser(String userName) {
		// 链接数据库
		Connection coon = ConnectionDB.getCoon();
		PreparedStatement ptmp = null;
		ResultSet res = null;
		// sql语句
		String sql = "select * from userinfo where username=? ";
		try {
			// 获取发送sql的对象
			ptmp = coon.prepareStatement(sql);
			// 传参
			ptmp.setString(1, userName);
			// 执行sql
			res = ptmp.executeQuery();
			if (res.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionDB.closeDB(coon, ptmp, res);
		}
		return false;
	}

	public void delete(String userName) {
		// 链接数据库
		Connection coon = ConnectionDB.getCoon();
		PreparedStatement ptmp = null;
		ResultSet res = null;
		// sql语句
		String sql = "delete from userinfo  where  username = ?";
		
		try {
			//获取发送sql的对象
			ptmp = coon.prepareStatement(sql);
			//传参
			ptmp.setString(1, userName);
			//执行sql
			int executeUpdate = ptmp.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				ptmp.close();
				coon.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

数据库链接(数据库信息采用的是配置文件的方式)
package com.student.db;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.sun.org.apache.regexp.internal.recompile;

import sun.net.www.content.text.plain;

public class ConnectionDB {
	private static String driver;
	private static String url;
	private static String user;
	private static String pass;
	
	static {
	//创建Properties类
	Properties p = new Properties();
	//加载配置文件
	ClassLoader classLoader = ConnectionDB.class.getClassLoader();
	InputStream is = classLoader.getResourceAsStream("db.properties");
	try {
		p.load(is);
		driver = p.getProperty("driver");
		url = p.getProperty("url");
		user = p.getProperty("username");
		pass = p.getProperty("password");
	} catch (IOException e) {
		e.printStackTrace();
	}
	}
	
	public static Connection getCoon() {
		Connection coon = null;
		try {
			//加载驱动
			Class.forName(driver);
			//链接数据库
			coon = DriverManager.getConnection(url, user, pass);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return coon;
	}
	
	public static void closeDB(Connection coon,Statement stmt,ResultSet res) {
		if (coon!=null) {
			try {
				coon.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (stmt!=null) {
			try {
				coon.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (res!=null) {
			try {
				coon.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static void closeDB(Connection coon,Statement stmt) {
		if (coon!=null) {
			try {
				coon.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (stmt!=null) {
			try {
				coon.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

数据库配置文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://你的ip:3306/你的数据库名称
username=用户名
password=密码
界面(Ui层)

1、StudentUi

package com.student.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import com.student.dao.StudentDao;
import com.student.entity.Student;
import com.student.ui.StudentInfoUI;
import com.student.utils.Utils;

import jdk.nashorn.internal.ir.Flags;

public class StudentForUI {
	static StudentDao studentDao = new StudentDao();

	public  void check() {

		Scanner scanner = new Scanner(System.in);

		StudentInfoUI studentInfoUI = new StudentInfoUI();
		studentInfoUI.logining();

		// 登陆中的界面

		System.out.println("登陆中");
		for (int i = 0; i < 2; i++) {
			System.out.print(">");
			try {
				Thread.currentThread().sleep(500);
			} catch (InterruptedException  e) {
				e.printStackTrace();
			}
		}
		System.out.println();
		int fun = 0;
		while (true) {

			System.out.println("-----请选择你要执行的功能-----");
			System.out.println("1:添加学生基本信息");
			System.out.println("2:查找学生");
			System.out.println("3:根据学号更新学生基本信息");
			System.out.println("4:根据学号删除学生");
			System.out.println("6:查看所有学生");
			System.out.println("7:排序");
			System.out.println("8:保存 学生信息");
			System.out.println("9:读取学生信息");
			System.out.println("10:退出系统");
			System.out.println("11:回到主菜单");
			System.out.println("----------------------------");

				try {				
					fun = Integer.parseInt(scanner.nextLine());
					if (fun<1||fun>9) {
						System.out.println("输入的数字不在范围内!请重新输入:");
					}
				}catch (NumberFormatException e) {
					System.out.println("输入的选项不合法!重新输入");
				}
			switch (fun) {
			case 1:// 添加学生

				add(scanner);
				pressContinue(scanner);
				break;
			case 2:// 查找学生
				
				findStu(scanner);
				break;

			case 3:// 更新学生信息

				update(scanner);
				pressContinue(scanner);
				break;
			case 4:// 根据学号删除学生

				delete(scanner);
				pressContinue(scanner);
				break;
			case 5:// 根据学生编号输入学生各门成绩

				pressContinue(scanner);
				break;

			case 6:// 查看学生

				getAll();
				pressContinue(scanner);
				break;

			case 7:// 排序学生

				System.out.println("B:总分降序    C:学号升序");
				String s2 = scanner.nextLine();
				switch (s2) {
				
				case "b":
				case "B":

					sortByStuNum();
					pressContinue(scanner);
					break;

				case "c":
				case "C":

					sortById();
					pressContinue(scanner);
					break;
				default:
					break;
				}
				break;
			case 8:// 保存信息
				savestu();
				pressContinue(scanner);
				break;
			case 9:// 读取信息
				
				pressContinue(scanner);
				break;
			case 10:// 退出系统
				System.exit(0);
				break;

			case 11:
				studentInfoUI.logining();
				break;

			default:
				break;
				
			}

		}
	}

	

	private void savestu() {
		ArrayList showAll = studentDao.showAll();
		studentDao.writeStu(showAll);
	}



	private void findStu(Scanner scanner) {
		
		System.out.println("A:学号查找      B:姓名查找");
		String s1 = scanner.nextLine();
		
		
		switch (s1) {
		case "a":
		case "A":
			find(scanner);
			pressContinue(scanner);
			break;
		case "b":
		case "B":
			findByName(scanner);
			pressContinue(scanner);
			break;

		default:
			System.out.println("请输入A和B");
			break;
		}
	}



	private static void sortById() {
		// TODO Auto-generated method stub
		ArrayList sortById = studentDao.sortById();
		System.out.println("\t学号\t姓名\t成绩一\t成绩二\t成绩三\t总分\t平均分");
		for(int i = 0;i<sortById.size();i++) {
			System.out.println(sortById.get(i));
		}
	}

	private static void sortByStuNum() {
		ArrayList sortById = studentDao.sortBySum();
		System.out.println("\t学号\t姓名\t成绩一\t成绩二\t成绩三\t总分\t平均分");
		for(int i = 0;i<sortById.size();i++) {
			System.out.println(sortById.get(i));
		}
		
		
	}

	private static void sortByScore(StudentDao studentDao) {
		// TODO Auto-generated method stub
		studentDao.sortBySum();
	}

	private static void findByName(Scanner scanner) {
		System.out.println("输入查找包含名字的关键字:");
		String keyName = scanner.nextLine();
		ArrayList serchByname = studentDao.serchByname(keyName);
		if (serchByname==null) {
			System.out.println("没找到");
		}else {
			System.out.println("找到有以下同学姓名包含"+keyName);
			System.out.println("\t学号\t姓名\t成绩一\t成绩二\t成绩三\t总分\t平均分");
			for(int i = 0;i<serchByname.size();i++) {
				System.out.println(serchByname.get(i));
			}
		}
	}

	private static void getAll() {
		// TODO Auto-generated method stub
		ArrayList<Student> stuList = studentDao.showAll();
		System.out.println("\t学号\t姓名\t成绩一\t成绩二\t成绩三\t总分\t平均分");
		for (int i = 0; i < stuList.size(); i++) {
			System.out.println(stuList.get(i));
		}
	}


	private static void delete(Scanner scanner) {
		boolean flag = false;
		do {
			flag = false;
		System.out.println("请输入删除的学生的学号");
		String num3 = scanner.nextLine();
		studentDao.serchStu(num3);
		Student serchStu = studentDao.serchStu(num3);
		
		if (serchStu==null) {
			System.out.println("没有该学生");
		}else {
			System.out.println("确定要删除吗?Y/N");
			String con = scanner.nextLine();
			if (con.equalsIgnoreCase("y")) {
				studentDao.deleteStu(num3);
				getAll();
			}
		}
		System.out.println("要继续删除吗Y/N?");
		String nextLine = scanner.nextLine();
		if (nextLine.equalsIgnoreCase("y")) {
			flag = true;
		}
		} while (flag);
	}

	private static void update(Scanner scanner) {
		boolean addflag = false;
		boolean isnull = false;
		String stuId = null;
		 do {
			 Student s = new Student();
			 addflag = false;
			 
			 do {
				 isnull = false;
				 System.out.println("输入要更新学生的学号:");
				 stuId = scanner.nextLine();
				 if (stuId.equals(" ")) {
					System.out.println("输入的学号不能为空");
					isnull = true;
				}else {
				 
				 studentDao.serchStu(stuId);
					Student serchStu = studentDao.serchStu(stuId);
					
					if (serchStu==null) {
						System.out.println("没有该学生");
						isnull = true;
					}
					}
			} while (isnull);
			 
			 
			
			 
			 
			 System.out.println("输入要更新加学生的姓名:");
			 String name = scanner.nextLine();
			 System.out.println("输入要更新加学生的sc1:");
			 int sc1 = Integer.parseInt(scanner.nextLine());
			 System.out.println("输入要更新加学生的sc2:");
			 int sc2 = Integer.parseInt(scanner.nextLine());
			 System.out.println("输入要更新加学生的sc3:");
			 int sc3 = Integer.parseInt(scanner.nextLine());
			 s.setStuId(stuId);
			 s.setName(name);
			 s.setSc1(sc1);
			 s.setSc2(sc2);
			 s.setSc3(sc3);
			 s.setAvg();
			 s.setSum();
			 boolean addStu = studentDao.updateSutdent(s);
			 ArrayList<Student> stuList = studentDao.showAll();
				System.out.println("\t学号\t姓名\t成绩一\t成绩二\t成绩三\t总分\t平均分");
				for (int i = 0; i < stuList.size(); i++) {
					System.out.println(stuList.get(i));
				}
			 if (addStu) {
				 System.out.println("更新成功,是否继续添加y/n");
			}
			 String input = scanner.nextLine();
			 if (input.equalsIgnoreCase("y")) {
					addflag = true;
			 }else {
					addflag = false;
			 }
		} while (addflag);
	}

	private static void find(Scanner scanner) {
		Student serchStu = null;
		String isContinue = "";
		boolean flag = false;
		
		do {
			flag = false;
		
		System.out.println("输入要查看的学号:");
		String stuId = scanner.nextLine();
		serchStu = studentDao.serchStu(stuId);
		if (serchStu==null) {
			System.out.println("你输入的学号不存在");
			System.out.println("是否继续查找Y/N");
		}else {
			System.out.println("\t学号\t姓名\t成绩一\t成绩二\t成绩三\t总分\t平均分");
			System.out.println(serchStu);
			System.out.println("是否继续查找Y/N?");
		}
		isContinue = scanner.nextLine();
		if (isContinue.equalsIgnoreCase("y")) {
			flag = true;
		}
		} while (flag);
	}

	//添加新的学生
	 private static void add(Scanner scanner)  {
		 boolean addflag = false;
		 boolean sameId = false;
		 boolean isnull = false;
		 String stuId = "";
		 do {
			 Student s = new Student();
			 addflag = false;
			 do {
			sameId = false;
			
			do {
				isnull = false;
				System.out.println("输入要新加学生的学号:"); 
				  stuId = scanner.nextLine();
				  if (stuId.equals(" ")) {
					System.out.println("学号不能为空!请重新输入:");
					isnull = true;
				}
			} while (isnull);
			 
			  
			 Student serchStu = studentDao.serchStu(stuId);
			 if (serchStu!=null) {
				System.out.println("学号重复,是否重新输入Y/N");
			}
			 String input = scanner.nextLine();
			 if (input.equalsIgnoreCase("y")) {
				 sameId = true;
			 }else {
				 sameId = false;
			 }
			 
			 } while (sameId);
			 
			 
			 System.out.println("输入要新加学生的姓名:");
			 String name = scanner.nextLine();
			 System.out.println("输入要新加学生的sc1:");
			 int sc1 = Integer.parseInt(scanner.nextLine());
			 System.out.println("输入要新加学生的sc2:");
			 int sc2 = Integer.parseInt(scanner.nextLine());
			 System.out.println("输入要新加学生的sc3:");
			 int sc3 = Integer.parseInt(scanner.nextLine());
			 s.setStuId(stuId);
			 s.setName(name);
			 s.setSc1(sc1);
			 s.setSc2(sc2);
			 s.setSc3(sc3);
			 s.setAvg();
			 s.setSum();
			 boolean addStu = studentDao.addStu(s);
			 if (addStu) {
				 System.out.println("添加成功,是否继续更新y/n");
			}
			 String input = scanner.nextLine();
			 if (input.equalsIgnoreCase("y")) {
					addflag = true;
			 }else {
					addflag = false;
			 }
		} while (addflag);
		 
		 
		 
	 }

	// 定义一个方法,实现 按下任意键继续
	public static void pressContinue(Scanner sc) {

		System.out.println("按下任意键继续....");
		sc.nextLine();

	}

}

2、StudentInfoUi

package com.student.ui;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Scanner;

import com.student.dao.StudentInfoDao;
import com.student.entity.StudentInfo;

public class StudentInfoUI {

	StudentInfoDao studentInfoDao = new StudentInfoDao();
	private static StudentInfo studentInfo = new StudentInfo();

	public void logining() {
		Scanner scanner = new Scanner(System.in);

		boolean logined = false;

		int fun = 0;
		do {
			logined = false;
			System.out.println("1:登录 ");
			System.out.println("2:注册 ");
			System.out.println("3:修改密码 ");
			System.out.println("4:注销账号 ");
			System.out.println("5:退出 ");
			System.out.println("请选择你要进行的操作:");
			try {
				fun = Integer.parseInt(scanner.nextLine());
				if(fun<1||fun>5) {
					System.out.println("数字范围不对!重新输入:");
					logined = true;
				}else {
					switch (fun) {
					case 1:

						boolean login1 = this.login1(scanner);
						if (login1) {
							System.out.println("登录成功");
							logined = false;
						}else {
							System.out.println("登录失败");
							logined = true;
						}
						pressContinue(scanner);
						break;

					case 2:

						this.register(scanner);
						pressContinue(scanner);
						break;
					case 3:

						this.mod(scanner);
						pressContinue(scanner);
						this.logining();
						break;
						
					case 4:
						this.del(scanner);
						pressContinue(scanner);
						this.logining();
						break;

					case 5:
						System.exit(0);
						break;

					default:
						break;
					}
				}
			} catch (NumberFormatException e) {
				System.out.println("只能输入数值!请重新输入");
				logined = true;
			}
			
			

		} while (logined);

	}

	private void del(Scanner scanner) {
		
		String userName = null;
		
		boolean isnull = false;
		do {
			isnull = false;
			System.out.println("请输入用户名:");
			userName = scanner.nextLine();
			if (userName.equals(" ")) {
				System.out.println("用户名不能为空!重新输入:");
				isnull = true;
			}
		} while (isnull);
		System.out.println("请输入要删除的密码:");
		String password = scanner.nextLine();
		StudentInfo login = studentInfoDao.login(userName, password);
		if (login!=null) {
			System.out.println("你确定要删除吗Y/N");
			String isdel = scanner.nextLine();
			if (isdel.equalsIgnoreCase("y")) {
				studentInfoDao.delete(userName);
			}
		}else{
			System.out.println("用户名或密码错误");
		}
		
		
	}

	private void mod(Scanner scanner) {
		String userName = null;
		boolean isnull = false;
		do {
			isnull = false;
			System.out.println("请输入用户名:");
			userName = scanner.nextLine();
			if (userName.equals(" ")) {
				System.out.println("用户名不能为空!重新输入:");
				isnull = true;
			}
		} while (isnull);
		String userPwd = null;
		StudentInfo user = null;
		boolean iscontain = studentInfoDao.getUser(userName);
		if (iscontain) {
			System.out.println("请输入新密码:");
			 userPwd = scanner.nextLine();
		}
		user = new StudentInfo(userName, userPwd);
		boolean issucess = studentInfoDao.modStudent(user);
		if (issucess) {
			System.out.println("修改成功");
		}
	}

	private void register(Scanner scanner) {
		// TODO Auto-generated method stub

		System.out.println("=========用户注册=========");

		String userName = null;
		String userPwd = null;
		StudentInfo user = null;
		String input = null;
		boolean flag = false;
		boolean user2 = false;
		boolean isnull = false;
		do {
			flag = false;
			user2 = false;
			
			do {
				isnull = false;
				System.out.println("请输入用户名:");
				userName = scanner.nextLine();
				if (userName.equals(" ")) {
					System.out.println("用户名不能为空!重新输入:");
					isnull = true;
				}
			} while (isnull);
			
			

			 user2 = studentInfoDao.getUser(userName);
			if (!user2) {
				flag = false;
			}else if(user2){
				System.out.println("用户名已经存在,请重新输入");
				flag = true;
				
			}
			

		} while (flag);

		System.out.println("请输入密码:");
		userPwd = scanner.nextLine();
		user = new StudentInfo(userName, userPwd);
		if (studentInfoDao.addStudent(user)) {
			System.out.println("注册成功,是否登录(Y/N)");
			input = scanner.nextLine();
			if (input.equalsIgnoreCase("y")) {
				this.login1(scanner);
			}else {
				this.logining();
			}
		}
	}

	private boolean login1(Scanner scanner) {
		// 设置标志
		
			System.out.println("登录界面=========(admin   123)");
			System.out.println("请输入账号:");
			String user = scanner.nextLine();
			System.out.println("请输入密码:");
			String pass = scanner.nextLine();

			StudentInfo login2 = studentInfoDao.login(user, pass);
			if (login2 != null) {
				return true;
			} else {
				return false;
			}
		
	}

	// 定义一个方法,实现 按下任意键继续
	public void pressContinue(Scanner sc) {

		System.out.println("按下任意键继续....");
		sc.nextLine();

	}

}

工具类
package com.student.utils;

import java.util.regex.Pattern;

public class Utils {

	public static boolean startWithNumber(String str){
		return Pattern.matches("[0-9].*", str);
		}

}

测试类
package com.student.test;

import com.student.ui.StudentForUI;
import com.student.ui.StudentInfoUI;

public class Test {

	public static void main(String[] args)  {
		StudentForUI studentTest = new StudentForUI();
		studentTest.check();
		
		StudentInfoUI studentInfoUI = new StudentInfoUI();
		studentInfoUI.logining();
	}

}

);
user = new StudentInfo(userName, userPwd);
if (studentInfoDao.addStudent(user)) {
System.out.println(“注册成功,是否登录(Y/N)”);
input = scanner.nextLine();
if (input.equalsIgnoreCase(“y”)) {
this.login1(scanner);
}else {
this.logining();
}
}
}

private boolean login1(Scanner scanner) {
	// 设置标志
	
		System.out.println("登录界面=========(admin   123)");
		System.out.println("请输入账号:");
		String user = scanner.nextLine();
		System.out.println("请输入密码:");
		String pass = scanner.nextLine();

		StudentInfo login2 = studentInfoDao.login(user, pass);
		if (login2 != null) {
			return true;
		} else {
			return false;
		}
	
}

// 定义一个方法,实现 按下任意键继续
public void pressContinue(Scanner sc) {

	System.out.println("按下任意键继续....");
	sc.nextLine();

}

}


#### 工具类

```java
package com.student.utils;

import java.util.regex.Pattern;

public class Utils {

	public static boolean startWithNumber(String str){
		return Pattern.matches("[0-9].*", str);
		}

}

测试类
package com.student.test;

import com.student.ui.StudentForUI;
import com.student.ui.StudentInfoUI;

public class Test {

	public static void main(String[] args)  {
		StudentForUI studentTest = new StudentForUI();
		studentTest.check();
		
		StudentInfoUI studentInfoUI = new StudentInfoUI();
		studentInfoUI.logining();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值