JAVA金沙官网线上 | 学生选课系统

这里使用JAVA语言编写的简易的学生选课系统,展现的都是这个系统核心代码。

package com.hanqi.telbook;

其中有不足欢迎批评和指正!

import java.util.Scanner;

 

public class Menu {
//主菜单

链接数据库的代码

public void mainMenu()
{

package connection;
//连接数据库student

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("** 6 退出记录 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:6");

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

}
//添加用户菜单
public void addMenu()
{

public class xu{

System.out.println("**************************");
System.out.println("** 1 添加新记录 **");
System.out.println("** 2 查看全记录 **");
System.out.println("** 3 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:3");

private static Connection conn = null;
static {
try {
// 注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 获得一个数据库连接
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=student","sa","123456");
}catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
//查找用户菜单
public int searchMenu()
{int rst=0;
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("** 6 查看全记录 **");
System.out.println("** 7 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:7");

public static Connection getConnection() {
return conn;
}
}

Scanner sc=new Scanner(System.in );

 

while(true)
{ try{
rst=sc.nextInt();
if(rst>=1&&rst<=7)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

 

catch(Exception e)
{
System.out.println("输入不合法,请重新输入:");
}
break;
}
return rst;
}
//修改用户信息主菜单
public void modifyMenu()
{
System.out.println("**************************");
System.out.println("** 1 查看全记录 **");
System.out.println("** 2 修改指定记录 **");
System.out.println("** 3 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:3");
}
//修改用户信息子菜单
public int subModifyMenu()
{
int rst=0;
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("** 6 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:6");
Scanner sc=new Scanner(System.in );
while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=6)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

选课的操作代码

return rst;

package connection;

}
//删除用户信息菜单
public int deleteMenu()
{
int rst=0;
System.out.println("**************************");
System.out.println("** 1 查看全记录 **");
System.out.println("** 2 删除指定记录 **");
System.out.println("** 3 删除全部记录 **");
System.out.println("** 4 返回上一级 **");
System.out.println("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:4");
Scanner sc=new Scanner(System.in );

import java.sql.*;
import java.util.*;

while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=4)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

public class test1{

return rst;
}
//排序用户信息菜单
public int orderMenu()
{
int rst=0;
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("**************************");
System.out.println("请输入正确的数字,最小是:1,最大是:5");
Scanner sc=new Scanner(System.in );

private static String ID; // 此处的ID设为全局变量,在下面某些方法里会用到
private static Scanner console = new Scanner(System.in);
private static String managerPass = "123456";
private static Connection conn = xu.getConnection();

while(true)
{
rst=sc.nextInt();
if(rst>=1&&rst<=5)
{
break;
}
else
{
System.out.println("输入错误,重新选择:");
}
}

public static void main(String [] args) throws SQLException {

return rst;
}

System.out.println("Welcome to the student selection system!nn"
+"if you are maneger please choose 6、7、8、9、10n"
+"if you are student Please choose 1、2、3、4、5、10n");
ShowMenu();
}

public void start() throws Exception
{
while(true)
{
TelNoteRegex tnr=new TelNoteRegex();
Operate op=new Operate();
int i=tnr.menuRegex(1, 6);
switch(i)
{//添加
case 1:
addMenu();
tnr.addmenu(1, 3);
break;
//查找
case 2:

public static void ShowMenu() throws SQLException {

op.searchLogic();
break;
//修改
case 3:

System.out.println("-----------option-------------n");

op.modifyLogicLogic();
break;
//删除
case 4:

System.out.println("1--Student login"); // 学生登录
System.out.println("2--Change password"); // 学生修改密码
System.out.println("3--Choose course"); // 学生选课
System.out.println("4--Get course list"); // 学生查看自己的课程表
System.out.println("5--Student rigistration"); // 学生注册账号
System.out.println("6--Create student"); // 管理员创建学生
System.out.println("7--Create course"); // 管理员创建课表
System.out.println("8--Get student list"); // 管理员查看学生表
System.out.println("9--Get course list1"); // 管理员查看选课表里的课程
System.out.println("10--exitn"); // 退出

op.deleteLogic();
break;
//排序
case 5:
op.orderLogic();
break;
//退出
case 6:

Test();
}

break;

public static void Test() throws SQLException {

default:
break;

System.out.println("manager choose 1 ,student choose 0");
int choose1 = console.nextInt(); //int 用nextInt()

}
if(i==6)

if(choose1 == 1) {

break;

System.out.println("Please input your password :");
String password = console.next(); //String 用next()

}
}
}

if(password.equals(managerPass)) {

package com.hanqi.telbook;
import java.util.Scanner;

System.out.println("Welcome manager!n");
ManagerMenu1();
}

public class TelNoteRegex {
//对菜单输入选项的验证

else {

public int menuRegex (int min, int max )
{
int scc=0;
try{
Menu me=new Menu();

System.out.println("Your password is worry!"); }
}

Scanner sc =new Scanner(System.in);
while(true)
{
me.mainMenu();
int i=sc.nextInt();
if(i>=min&&i<=max&&i%1==0)
{
scc=i;
break;
}
}
}

else if(choose1 == 0) {

catch(Exception e)
{
System.out.println("输入的数字不合法");

System.out.println("Welcome Student!");
StudentMenu1();
}
}

}
return scc;
}
//添加用户的子菜单
public int addmenu(int min,int max)throws Exception
{
int scc=0;
try{
TelNoteRegex tnr=new TelNoteRegex();
Operate op=new Operate();
Scanner sc =new Scanner(System.in);
while(true)
{

/*管理员操作菜单*/
public static void ManagerMenu1() throws SQLException {

int i=sc.nextInt();

System.out.println("----------Manager----------n");
System.out.println("Please choose option");
int choose2 = console.nextInt();

if(i>=min&&i<=max&&i%1==0)
{
scc=i;
switch(i)
{
case 1:
op.addOperation();
break;
case 2:
op.showAll();

switch(choose2) {

break;
case 3:
Menu mee=new Menu();
mee.start();
break;
default:
break;
}

case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
System.out.println("This is student`s option,try again!");
ManagerMenu1();
break;
case 6 : CreateStu();break;
case 7 : CreateCourse();break;
case 8 : GetStudentList();break;
case 9 : GetCourseList1();break;
case 10 : System.exit(0);break;
default : System.out.println("Please input an right number , try again!"); ManagerMenu1();

return scc;

}
}

}
break;
}
}
catch(Exception e)
{
System.out.println("输入的数字不合法");

/*学生操作菜单*/
public static void StudentMenu1() throws SQLException {

}
return scc;
}
//对用户输入姓名的验证
public String nameRegex()
{
String name="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入姓名:1-10位字母");
String str=sc.nextLine();
if(str.matches("[a-zA-Z]{1,10}"))
{
name=str;
break;
}
}
return name;
}
//对用户输入年龄的验证
public int ageRegex()
{
int age=0;
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入年龄,1-100+");
int ii=sc.nextInt();
if(ii>0&&ii<=100)
{
age=ii;
break;
}
}
return age;
}
// 对用户输入性别的验证
public String sexRegex()
{
String sex="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入性别,(男 m or M)(女 f or F)");
String str=sc.nextLine();
if(str.equals("男")||str.equals("m")||str.equals("M")
||str.equals("女")||str.equals("f")||str.equals("F"))
{
sex=str;
break;

System.out.println("----------Student----------n");
System.out.println("Please input option");
int choose3 = console.nextInt();

}
}
return sex;
}
//对用户输入电话号码的验证
public String telNumRegex()
{
String tel="";
while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入电话号码,6-10位数字");
String tell=sc.nextLine();
if(tell.matches("[0-9]{6,10}"))
{
tel=tell;
break;
}
}
return tel;
}
//对用户输入地址的验证
public String addressRegex()
{
String add="";

switch(choose3) {

while(true)
{
Scanner sc =new Scanner(System.in);
System.out.println("输入地址,1-50位字母或数字");
String str=sc.nextLine();
if(str.matches("[A-Za-z0-9]{1,50}"))
{
add=str;
break;
}
}
return add;
}

case 1 : StuLogin();break;
case 2 : ChangePass();break;

}

//在进行Choose course,Get course list之前都要求学生进行StuLogin操作

package com.hanqi.telbook;

case 3 :
case 4 : System.out.println("you must login before you can do it!n");StuLogin();break;
case 5 : StuRigistration();break;
case 6 :
case 7 :
case 8 :
case 9 : System.out.println("This is manager`s option,try again!");StudentMenu1();break;
case 10 : System.exit(0);break;
default : System.out.println("Please input an right number,try again!");StudentMenu1();

public class Person {
private int id;
private String name;
private int age;
private String sex;
private String telnum;
private String address;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTelnum() {
return telnum;
}
public void setTelnum(String telnum) {
金沙官网线上,this.telnum = telnum;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}
}

public Person(String name, int age, String sex, String telnum, String address) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.telnum = telnum;
this.address = address;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [序号=" + id + ", 姓名=" + name + ", 年龄=" + age + ", 性别=" + sex + ", 电话号码=" + telnum

/*管理员创建课程*/
public static void CreateCourse() throws SQLException{

  • ", 地址=" + address + "]";
    }

try {
/* 通过connection数据库链接对象 创建一个statement对象数据库操作对象,stat对象再获取一个执行sql的语句。
* stat对象就是java程序与Database的一个连接通道*/

}

Statement stat = conn.createStatement();
String sql1 = "insert into course(couID,couName,couTeacher)values(1,'软件工程','黄伟国')";
String sql2 = "insert into course(couID,couName,couTeacher)values(2,'数据库','韦美雁')";
String sql3 = "insert into course(couID,couName,couTeacher)values(3,'算法设计','黎明')";
String sql4 = "insert into course(couID,couName,couTeacher)values(4,'大学英语','章琴')";
String sql5 = "insert into course(couID,couName,couTeacher)values(5,'Java','唐雅媛')";

package com.hanqi.telbook;

int a = stat.executeUpdate(sql1);
int b = stat.executeUpdate(sql2);
int c = stat.executeUpdate(sql3);
int d = stat.executeUpdate(sql4);
int e = stat.executeUpdate(sql5);

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 java.util.List;
import java.util.Scanner;
import com.mchange.v2.c3p0.ComboPooledDataSource;

int sum = a + b + c + d + e;
System.out.println("成功添加"+ sum + "行课程数据");
ManagerMenu1();

public class Operate {

} catch (SQLException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("Course already exists");

ComboPooledDataSource cp=new ComboPooledDataSource("helloc3p0");
//list 集合
private List<Person> ls= new ArrayList<>();

ManagerMenu1();
}
}

//用户添加信息业务逻辑控制
public void addLogic()
{
TelNoteRegex tnr=new TelNoteRegex();
//配置连接池
try {

/*管理员创建学生*/
public static void CreateStu() throws SQLException {

Connection conn=cp.getConnection();
String sql="insert into person values(sq_person.nextval,?,?,?,?,?)";
PreparedStatement ps= conn.prepareStatement(sql);

try {
Statement stat = conn.createStatement();
String sql1 = "insert into student(stuID,stuName) Values(20160501,'徐明正')";

ps.setString(1, tnr.nameRegex());
ps.setInt(2, tnr.ageRegex());
ps.setString(3, tnr.sexRegex());
ps.setString(4, tnr.telNumRegex());
ps.setString(5, tnr.addressRegex());
ps.executeUpdate();
System.out.println("添加账户成功");
ps.close();
conn.close();
cp.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//用户查询信息业务逻辑控制
public List<Person> searchLogic() throws Exception
{
Menu me=new Menu();
List<Person> p=null;
switch(me.searchMenu())
{
case 1:
searchByName();

// 这里对学生信息的输入也必须一个一个输入,这是系统的缺陷,问题待解决

break;
case 2:
searchByAge();

int a = stat.executeUpdate(sql1);
System.out.println("成功添加"+ a + "行学生数据");
ManagerMenu1();

break;
case 3:
searchBySex();

}catch(SQLException e) {
// TODO Auto-generated catch block
System.out.println("The student already exists");

break;
case 4:
searchByTelNum();

ManagerMenu1();
}
}

break;
case 5:
searchByAdd();

/*学生登录*/
public static void StuLogin() throws SQLException {

break;
case 6:
showAll();

System.out.println("Please input your stuID"); // 输入账号
ID = console.next();

break;
case 7:

System.out.println("Please input your Password"); // 输入密码
String stuPassword = console.next();

break;

String stuID = null;
String Password = null;

default:
break;
}

Statement stat ;
ResultSet rs ;

return p;
}

try {
stat = conn.createStatement();
String sql1 = "select * from login "; /* 从login表中查询其所有属性值,则stuID 和 Password都有了。
rs是结果集。查询出的记录是一个列表,初始时指针指向的是第一条记录之前的。*/
boolean flag;
rs = stat.executeQuery(sql1);

//修改信息业务逻辑控制
public void modifyLogicLogic() throws Exception
{
try{
Menu me=new Menu();
me.modifyMenu();
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if(i==1)
{
showAll();
}
else if(i==2)
{Menu mee=new Menu();

while(flag = rs.next()) { // rs.next(); //返回值为true or false

switch(mee.subModifyMenu())
{
case 1:

stuID = rs.getString("stuID");
Password = rs.getString("Password");

Scanner sc2=new Scanner(System.in);

if( ID.equals(stuID) && stuPassword.equals(Password)) // 注意这里,跳出循环的条件很重要

System.out.println("请输入需要修改的ID号");
int id=sc2.nextInt();
modifyname( id);
break;
case 2:

break;

Scanner sc3=new Scanner(System.in);

// 如果在数据库里没有找到与用户输入的stuID和Password相匹配的stuID和Password,flag则变为false

System.out.println("请输入需要修改的ID号");
int id1=sc3.nextInt();
modifyage( id1);
break;
case 3:

}

Scanner sc4=new Scanner(System.in);

if(!flag){ // 当flag 为 false 时

System.out.println("请输入需要修改的ID号");
int id2=sc4.nextInt();

System.out.println("The ID or Password is worry!Please input your ID and Password againn");
StuLogin();
}

modifysex(id2);
break;
case 4:

else {
System.out.println("***"+ID+"***"+stuID +"***"+stuPassword+"***"+Password+"***");

Scanner sc5=new Scanner(System.in);

System.out.println("Login Successful!n");
System.out.println("------------------Please choose option------------------n"
// +"If you want to Change Password ,Please choose 2n"
+"If you want to Choose Course,Please choose 3n"+"If you want to Get Course List,Please choose 4n"
+"If you want to exit ,Please choose 8n");

System.out.println("请输入需要修改的ID号");
int id3=sc5.nextInt();
modifytelnum( id3);
break;
case 5:

int choose4 = console.nextInt();

Scanner sc6=new Scanner(System.in);

switch(choose4) {

System.out.println("请输入需要修改的ID号");
int id4=sc6.nextInt();
modifyaddress( id4);
break;
case 6:
modifyLogicLogic();
break;

// case 2 : ChangePass();break;
case 3 : ChooseCourse();break;
case 4 : GetCourseList();break;
case 10 : System.exit(0);break;
default : System.out.println("You input an invalid number!");

}
}

}

else if(i==3)
{

}

me.start();

} catch (SQLException e) {

}
}
catch(Exception e)
{
System.out.println("输入不合法,请重新输入!");
}
}

本文由金沙官网线上发布于数据库,转载请注明出处:JAVA金沙官网线上 | 学生选课系统

您可能还会对下面的文章感兴趣: