学生成绩管理系统
create table student
( stdtId char(6),--学号,只能是6位
stdtName varchar(10),--姓名
class char(4),--班级,只能是4位
grade int check(grade>=0 and grade<=100),--学生成绩,必须在0和100之间
primary key(stdtId)--主键
);
create table teacher
( tcrId char(6),--教师编号,只能是6位
tcrName varchar(10),--教师姓名
primary key(tcrId)--主键
);
insert into student values('201801','见血封喉','1801',89);
insert into student values('201811','独孤求败','1801',77);
insert into student values('201823','东方彧卿','1802',63);
insert into student values('201824','喋喋不休','1802',96);
insert into student values('201807','走马观花','1803',90);
insert into student values('201809','花天酒地','1803',98);
insert into student values('201833','地大物博','1804',83);
insert into student values('201861','博君一笑','1804',81);
insert into student values('201884','笑口常开','1805',76);
insert into student values('201895','开天辟地','1805',92);
insert into teacher values('201607','滴水穿石');
insert into teacher values('201611','石破天惊');
insert into teacher values('201606','经天纬地');
package database;
import java.sql.DriverManager;
import java.sql.Connection;
public class connection {
public static Connection getConnection() {
String url="jdbc:sqlserver://LAPTOP-2FMIL3F1:1433;"+"DatabaseName=csdn";
String user="sa";
String password="1234";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//System.out.println("数据库驱动加载成功!");
Connection con = DriverManager.getConnection(url,user,password);
if(null!=con) {
//System.out.println("数据库连接成功!!");
return con;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package database;
import java.sql.PreparedStatement;
public class preparedStatement {
public static PreparedStatement getPreparedStatement(String sql) {
try {
PreparedStatement pstmt=connection.getConnection().prepareStatement(sql);
return pstmt;
} catch(Exception e) {
e.getStackTrace();
}
return null;
}
}
package database;
import java.sql.ResultSet;
public class resultSet {
public static ResultSet getResultSet(String sql) {
try {
ResultSet rs=preparedStatement.getPreparedStatement(sql).executeQuery();
return rs;
} catch(Exception e) {
e.getStackTrace();
}
return null;
}
}
package database;
import java.sql.ResultSet;
import java.sql.SQLException;
public class query {
public static void queryStudent(String stdtId) {
String sql="select * from student where stdtId='"+stdtId+"'";
ResultSet rs=resultSet.getResultSet(sql);
System.out.print("学号\t");
System.out.print("姓名\t");
System.out.print("班级\t");
System.out.println("成绩");
try {
while(rs.next()) {
System.out.print(rs.getString("stdtId")+"\t");
System.out.print(rs.getString("stdtName")+"\t");
System.out.print(rs.getString("class")+"\t");
System.out.println(rs.getInt("grade"));
}
}catch(SQLException e) {
System.err.println("查询出错 "+e.getStackTrace());
}
}
public static void queryTeacher(String tcrId) {
String sql="select * from teacher where tcrId='"+tcrId+"'";
ResultSet rs=resultSet.getResultSet(sql);
System.out.print("教师编号\t");
System.out.println("姓名");
try {
while(rs.next()) {
System.out.print(rs.getString("tcrId")+"\t");
System.out.println(rs.getString("tcrName"));
}
}catch(SQLException e) {
System.err.println("查询出错 "+e.getStackTrace());
}
}
public static void queryStudent() {
String sql="select * from student";
ResultSet rs=resultSet.getResultSet(sql);
System.out.print("学号\t");
System.out.print("姓名\t");
System.out.print("班级\t");
System.out.println("成绩");
try {
while(rs.next()) {
System.out.print(rs.getString("stdtId")+"\t");
System.out.print(rs.getString("stdtName")+"\t");
System.out.print(rs.getString("class")+"\t");
System.out.println(rs.getInt("grade"));
}
}catch(SQLException e) {
System.err.println("查询出错 "+e.getStackTrace());
}
}
}
package heaven;
import menu.mainMenu;
public class Main {
public static void main(String[] args) {
mainMenu.menu();
}
}
package login;
import java.sql.ResultSet;
import java.sql.SQLException;
import database.resultSet;
public class stdtLogin {
public static boolean login(String stdtId, String stdtName) {//学生登录,把学生姓名当作登录密码
String sql="select stdtName from student where stdtId='"+stdtId+"'";
int idExist=0;//账户不存在
try {
ResultSet rs=resultSet.getResultSet(sql);
if(rs.next()) {
idExist=1;
if(stdtName.equals(rs.getString("stdtName"))) {
return true;
}
}
}catch(SQLException e) {
System.err.println("学生登录出错 "+e.getStackTrace());
}
if(idExist==0) {
System.err.println("账户不存在!!");
}else {
System.err.println("密码错误!!");
}
return false;
}
}
package login;
import java.sql.ResultSet;
import java.sql.SQLException;
import database.resultSet;
public class tcrLogin {
public static boolean login(String tcrId, String tcrName) {
String sql="select tcrName from teacher where tcrId='"+tcrId+"'";
int idExist=0;//账户不存在
try {
ResultSet rs=resultSet.getResultSet(sql);
if(rs.next()) {
idExist=1;
if(tcrName.equals(rs.getString("tcrName"))) {
return true;
}
}
}catch(SQLException e) {
System.err.println("教师登录出错 "+e.getStackTrace());
}
if(idExist==0) {
System.err.println("账户不存在!!");
}else {
System.err.println("密码错误!!");
}
return false;
}
}
package menu;
import java.util.Scanner;
public class mainMenu {//主菜单
public static void menu() {
System.out.println("\n欢迎来到天堂大学\n");
System.out.println("1.学生登录");
System.out.println("2.教师登录");
System.out.println("3.退出");
Scanner input=new Scanner(System.in);
System.out.println("请输入对应操作的序号:");
int oprtNumber=0;
if(input.hasNextInt()) {
oprtNumber=input.nextInt();
}
switch(oprtNumber) {
case 1:
System.out.println("您选择了学生登录!!");
studentLoginMenu.menu();
break;
case 2:
System.out.println("您选择了教师登录!!");
teacherLoginMenu.menu();
break;
case 3:
System.out.println("感谢光临天堂大学!!");
break;
default:
System.err.println("您的输入有误,请重新输入!!");
menu();
}
input.close();
}
}
package menu;
import database.query;
public class stdtMenu {
public static void menu(String stdtId) {
System.out.println("你的信息如下:");
query.queryStudent(stdtId);
mainMenu.menu();
}
}
package menu;
import java.util.Scanner;
import login.stdtLogin;
public class studentLoginMenu {//学生登录菜单
public static void menu() {
String stdtId="";
String stdtName="";
Scanner input=new Scanner(System.in);
System.out.println("请输入学号:");
if(input.hasNextLine()) {
stdtId=input.nextLine();
}
System.out.println("请输入姓名:");
if(input.hasNextLine()) {
stdtName=input.nextLine();
}
if(stdtLogin.login(stdtId, stdtName)) {
System.out.println("登录成功!!!");
stdtMenu.menu(stdtId);//登录成功,跳转到学生菜单
}else {
System.err.println("登录失败!!!");
mainMenu.menu();//登录失败,返回主菜单
}
input.close();
}
}
package menu;
import database.query;
public class tcrMenu {
public static void menu() {
System.out.println("所有的学生信息如下:");
query.queryStudent();
mainMenu.menu();
}
}
package menu;
import java.util.Scanner;
import login.tcrLogin;
public class teacherLoginMenu {
public static void menu() {
String tcrId="";
String tcrName="";
Scanner input=new Scanner(System.in);
System.out.println("请输入教师编号:");
if(input.hasNextLine()) {
tcrId=input.nextLine();
}
System.out.println("请输入姓名:");
if(input.hasNextLine()) {
tcrName=input.nextLine();
}
if(tcrLogin.login(tcrId, tcrName)) {
System.out.println("登录成功!!!");
tcrMenu.menu();//登录成功,跳转到教师菜单
}else {
System.err.println("登录失败!!!");
mainMenu.menu();//登录失败,返回主菜单
}
input.close();
}
}