JDBC实现学生信息管理系统——学生信息管理系统

package com.jd.util;

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

public class StudentSystem {

	static boolean arrow = true;

	public static void main(String[] args) {
		System.out.println("*******************************");
		System.out.println("*                             *");
		System.out.println("*   欢迎进入学生信息管理系统    *");
		System.out.println("*                             *");
		System.out.println("*******************************");
		System.out.println("请先登录");

		for (;;) {
			menu();
		}
	}

	public static void menu() {

		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);

		if (arrow) {
			System.out.println("请输入用户名:");
			String username = scanner.nextLine();
			System.out.println("请输入密码:");
			String password = scanner.nextLine();
			String str = "select id from user_info where user_name = ? and password = ? ";
			class RowMapper implements IRowMapper {

				boolean flag;

				@Override
				public void rowMapper(ResultSet resultSet) {
					try {
						flag = resultSet.next();
					} catch (SQLException e) {
						e.printStackTrace();
					}

				}
			}
			RowMapper rowMapper = new RowMapper();
			DBUtil.select(str, rowMapper, username, password);
			if (rowMapper.flag) {
				System.out.println("登录成功!");
				arrow = false;

			} else {
				System.out.println("登录失败!");
				return;
			}
		}
		System.out.println("1.添加学生信息");
		System.out.println("2.删除学生信息");
		System.out.println("3.修改学生信息");
		System.out.println("4.查询学生信息");
		System.out.println("请输入操作序号,以Enter结束:");
		int option = scanner.nextInt();
		switch (option) {
		
		case 1: {// 添加成员

			System.out.println("请输入学生学号:");
			String id = scanner.next();

			if (DBUtil.exist("select name from student where id = ? ", id) > 0) {
				System.out.println("请输入学生姓名:");
				String name = scanner.next();
				System.out.println("请输入学生电话:");
				String mobile = scanner.next();
				DBUtil.update("insert into student (id,name,mobile) values(?,?,?)", id, name, mobile);
				System.out.println("添加成功");
			} else {
				System.out.println("学号重复,无法添加!");
			}
			break;
		}
		
		case 2: {
			System.out.println("请输入学生学号:");
			String id = scanner.next();
			if (DBUtil.update("delete from student where id = ?", id)) {
				System.out.println("删除成功!");
			} else {
				System.out.println("学号不存在,无法删除!");
			}
			break;
		}

		case 3: {
			System.out.println("请输入学生学号:");
			String id = scanner.next();

			if (DBUtil.exist("select name from student where id = ? ", id) < 0) {
				System.out.println("请输入学生姓名:");
				String name = scanner.next();
				System.out.println("请输入学生电话:");
				String mobile = scanner.next();
				DBUtil.update("update student set name= ?,mobile= ? where id= ?", name, mobile, id);
				System.out.println("修改成功!");
			} else {
				System.out.println("学号不存在!");
			}
			break;
		}
		
		case 4: {
			System.out.println("请输入学生学号:");
			String id = scanner.next();
			
			DBUtil.select("select *from student where id = ?",(ResultSet resultSet) -> {
				try {

					if(resultSet.next()) {
						String name = resultSet.getString("name");
						String mobile = resultSet.getString("mobile");
						System.out.println("id="+id+ ",name=" + name + ",mobile=" + mobile);
					}else {
						System.out.println("学生不存在!");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}, id);
			break;
		}

		default:
			break;
		}

	}

}

 

你可能感兴趣的:(Java课程)