学生成绩管理系统
- 关键词:java,SQL Server 2012
后端
- 打开SQL Server 2012:
- 点击加号:
- 选中数据库,右键,新建数据库:
- 点击确定,等待片刻
- 选中数据库,右键,刷新,即可看到:
建表
- 点击新建查询:
- 选择刚才新建的数据库,不然新建的表就存到别的数据库了,默认是master:
- 写建表语句:
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','经天纬地');
- 点击执行
- 后端我们就先写到这里,虽然这家学校只有10名同学,3位老师
- 但是,我们测试什么的已经够用了
前端
写代码
- 打开eclipse,新建项目,命名为student:
- 右键src,新建一个package,命名为database(这里面我们打算写和数据库连接有关的类):
- 右键database,新建三个class,分别命名为:connection,preparedStatement,resultSet:
- 其中connection里面写:
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;}
}
- 这个类的代码解析可以查看我的另一篇博客:如何使用JDBC连接数据库
- preparedStatement里面写:
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;}}
- resultSet里面写:
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;}}
- 其它包和类如下图所示:
- query里面写:
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());}}
}
- Main里面写:
package heaven;import menu.mainMenu;public class Main {public static void main(String[] args) {mainMenu.menu();}
}
- stdtLogin里面写:
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;}
}
- tcrLogin里面写:
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;}}
- mainMenu里面写:
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(); }
}
- stdtMenu里面写:
package menu;import database.query;public class stdtMenu {public static void menu(String stdtId) {System.out.println("你的信息如下:");query.queryStudent(stdtId);mainMenu.menu();}
}
- studentLoginMenu里面写:
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();}
}
- tcrMenu里面写:
package menu;import database.query;public class tcrMenu {public static void menu() {System.out.println("所有的学生信息如下:");query.queryStudent();mainMenu.menu();}
}
- teacherLoginMenu里面写:
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();}
}
添加jar包
- 下载SQL Server 2012的驱动jar包
- 项目右键,build path,Add External Archives,找到刚下载的jar包即可:
大功告成
- 双击Main,运行即可:
备注
- 有什么地方不明白的,可以评论区问我一下
- 这只是一个简单的模板,大家可以在里面添加功能的,即在相应的包里面添加类就行了
- 这是个例子,写得不好,可以重构一下