此次课程设计的题目是学生宿舍管理系统
数据库用的是SQL server;IDE用的是eclipse
为了便于对学生宿舍生活的管理,学生资料的管理,和快节奏、高效率的工作环境。我本次设计的课题是学生宿舍管理系统。
开发目的和意义:
A.管理学生宿舍信息达到安全、高效、方便效果
B.迅速查到所需学生、楼栋、宿舍、宿舍物品、宿舍维修等信息
C.学生宿舍管理系统便于管理人员办事效率越来越高
系统的作用:
A.要求用户必须输入正确的用户名和密码才可以进入系统
B.提供学生住宿情况、楼栋、宿舍、宿舍物品、宿舍维修的基本登记
C.可对上述的宿舍相关信息进行增删查改并统计
基本功能
A.宿舍信息维护:对宿舍信息进行添加、修改、删除,具体宿舍信息包括:宿舍编号、宿舍电话、应住人数、录入日期等信息;
B.宿舍信息查询:根据楼号、宿舍编号进行查询。具体包括:宿舍编号、应住人数、实住人数、空位数、宿舍电话、录入日期等信息;
C.宿舍信息统计:对楼号、房间数、应住人数、实住人数、空位数等进行统计并输出。
本宿舍管理系统通过管理员登录系统后,进行对学生信息、楼栋信息、宿舍信息、物品信息、维修信息,基本信息等进行管理(如图所示)
(分析系统中有哪些实体,实体之间存在怎样的联系,用文字描述,并绘制实体图和整体E-R图)
答:实体为楼栋、宿舍、学生、宿舍物品
楼栋实体图
(分析系统中有哪些实体,实体之间存在怎样的联系,用文字描述,并绘制实体图和整体E-R图)
答:实体为楼栋、宿舍、学生、宿舍物品
楼栋实体图
create table LgTbl
(
usrName char(10),
usrPwd char(10)
)
dbo.Build
create table Build
(
Bno char(4) primary key not null, --楼号-学3
Bfloors_sum char(4) , --楼层数-6层
Broom_sum char(4) , --宿舍数-30间
Build_men int , --总人数-1066人
build_sex char(6) , --男/女宿舍-男生宿舍
inputtime char(11) --2021-02-01
)
dbo.Room
create table Room
(
Bno char(4) not null, --楼号 -A
Rno char(6) not null , --宿舍号 -606
Rymen int , --宿舍几人间 -6人间
Rphone char(11), --宿舍电话- 13672355529
inputtime char(11) , --2021-02-01
primary key(Rno,Bno),
foreign key(Bno) references Build(Bno),
)
dbo.Student
create table Student
(
Sno char(12) primary key not null, --学号 -201955555500
Sname char(8) , --姓名-牛大春
Ssex char(2) , --性别 -男
Sdept char(10) , --专业 -自动化
Bno char(4) not null, --楼号 -学3
Rno char(6) not null , --宿舍号 -608
inputtime char(11) , --2021-02-01
foreign key(Bno) references Build(Bno),
)
dbo.Goods
create table Goods
(
Pno char(3) primary key, --物品号
Pname char(10) --物品名称
)
dbo.Repair
create table Repair
(
Bno char(4) not null,
Rno char(6) not null ,
Pno char(3) not null,
Rsubmit char(10), --提交日期
Rsolove char(10), --解决日期
Rreason char(20), --维修原因
primary key(Bno,Rno,Pno),
foreign key(Bno) references Build(Bno),
foreign key(Pno) references Goods(Pno),
)
JDBCUnit.java
package room_system;
import java.security.PublicKey;
import java.sql.*;
public class JDBCUnit {
/* 驱动,定义连接数据库类型:SQL SERVER2012*/
private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//驱动:com.microsoft.sqlserver.jdbc.SQLServerDriver
public static String SerName; //服务器名字:127.0.0.1
public static String Dbname; //数据库名:room_system
public static String userName; //用户名:sa
public static String userPwd; //密码:123456
public static Connection getConnection(){
String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=room_system";
Connection conn = null;
try {
//1.注册驱动
Class.forName(driverName);
//2.建立连接
conn = DriverManager.getConnection(dbURL, "sa", "123456");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 使用statement对象执行DML语句
*/
public static void stmt_DML(String sql) {
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
//1.创建Statement对象
stmt = conn.createStatement();
//2.执行sql语句
stmt.execute(sql);
//3.关闭资源
stmt.close(); //关闭数据执行结果集合
conn.close(); //关闭数据库连接
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 使用PreparedStatement执行SQL语句
*/
public static void ps_DML(String sql,ResultSet rs) {
try
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
//4.操作结果集
rs=ps.executeQuery();
//5.关闭资源
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 判断登录用户是否存在
*/
public static boolean login_flag(String usrName,String usrPwd) {
String sql= "select * from LgTbl where usrName="+"'"+usrName+"'"+" and usrPwd= "+"'"+usrPwd+"'";
boolean flag=false;
try
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
flag = true;
}
else {
flag = false;
}
rs.close();
ps.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace();
}
return flag;
}
/**
* 判断查询输入学号Sno结果是否为空
*/
public static boolean select_flag(String Sno) {
String sql= "select Sno from Student where Sno=ltrim(rtrim("+"'"+Sno+"'))";
boolean flag=false;
try
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
flag = true;
}
else {
flag = false;
}
rs.close();
ps.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace();
}
return flag;
}
/**
* 判断查询输入楼栋结果是否为空
*/
public static boolean select_Bno(String Bno) {
String sql= "select Bno from Room where Bno=ltrim(rtrim("+"'"+Bno+"'))";
boolean flag=false;
try
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
flag = true;
}
else {
flag = false;
}
rs.close();
ps.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace();
}
return flag;
}
/**
* 判断查询输入宿舍结果是否为空
*/
public static boolean select_Rno(String Rno) {
String sql= "select Rno from Room where Rno=ltrim(rtrim("+"'"+Rno+"'))";
boolean flag=false;
try
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
flag = true;
}
else {
flag = false;
}
rs.close();
ps.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace();
}
return flag;
}
/**
* 判断查询输入维修楼栋结果是否为空
*/
public static boolean select_weixiuBno(String Bno) {
String sql= "select Bno from Repair where Bno=ltrim(rtrim("+"'"+Bno+"'))";
boolean flag=false;
try
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
flag = true;
}
else {
flag = false;
}
rs.close();
ps.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace();
}
return flag;
}
/**
* 判断查询输入维修宿舍结果是否为空
*/
public static boolean select_weixiuRno(String Rno) {
String sql= "select Rno from Repair where Rno=ltrim(rtrim("+"'"+Rno+"'))";
boolean flag=false;
try
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
flag = true;
}
else {
flag = false;
}
rs.close();
ps.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace();
}
return flag;
}
/**
* 判断查询输入物品编号结果是否为空
*/
public static boolean select_Pno(String Pno) {
String sql= "select Pno from Goods where Pno=ltrim(rtrim("+"'"+Pno+"'))";
boolean flag=false;
try
{
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
flag = true;
}
else {
flag = false;
}
rs.close();
ps.close();
conn.close();
} catch (Exception e)
{
e.printStackTrace();
}
return flag;
}
}
RoomDemo.java
package room_system;
import static room_system.JDBCUnit.*;
import java.sql.Connection;
import java.util.Scanner;
public class RoomDemo {
public static void main(String[] args) {
String s1;
String[] s2;
/* 用户登录系统 */
String usrName;//登录名
String usrPwd;//登录密码
System.out.println("请按登录名,登录密码的格式输入数据连接信息(用,分隔):");
Scanner loginUsr = new Scanner(System.in);
s1 = loginUsr.next(); //输入的值赋给s1
s2= s1.split(","); //以’,'作为分隔符
usrName = s2[0];
usrPwd = s2[1];
if(login_flag(usrName,usrPwd)) //判断登录用户是否存在
{
System.out.println(" ");
System.out.println("*********************************");
System.out.println("* 用户登录成功! *");
System.out.println("* 欢迎使用学生信息管理系统! *");
System.out.println("*********************************");
System.out.println(" ");
}
else
{
System.out.println("********************************");
System.out.println("* 用户名或密码不正确! *");
System.out.println("********************************");
return ;
}
/* 主菜单 */
showMenu.show(); //调用showMenu的show方法,显示菜单
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("请输入您要执行操作的序号:");
int s ;
while(true) {
try {
s = sc.nextInt(); //把输入的序号赋值给s
break;
} catch (Exception e) {
System.out.println("输入格式错误,请重新输入!");
sc.nextLine();
}
}
switch (s) {
case 1:
Student_OP.select_allstudent(); //1.调用Student_OP所有学生信息的方法select_all()
break;
case 2:
Student_OP.add_stu(); // //2.添加入住学生的宿舍信息的方法add_stu()
break;
case 3:
Student_OP.alter_stu(); //3.更新入住学生的宿舍信息的方法alter_stu()
break;
case 4:
Student_OP.delete_stu(); //4.删除学生信息的方法delete_stu()
break;
case 5:
Student_OP.select_stu(); //5.查询指定学生的宿舍信息的方法select_stu()
break;
case 6:
Build_OP.select_allbuild(); //6.查询所有楼栋的信息的方法select_allbuild()
break;
case 7:
Build_OP.alter_build(); //7.更新指定楼栋的信息的方法alter_build()
break;
case 8:
Build_OP.select_build(); //8.查询指定楼栋的信息的方法select_build()
break;
case 9:
Room_OP.select_allroom(); //9.查询所有宿舍的信息的方法select_allroom()
break;
case 10:
Room_OP.alter_room(); //10.更改指定楼栋宿舍的信息的方法alter_room()
break;
case 11:
Room_OP.select_room() ; //11.查询指定楼栋宿舍的信息的方法select_room()
break;
case 12:
Room_OP.add_room(); //12.添加指定的楼栋宿舍信息的方法add_room()
break;
case 13:
Goods_OP.select_allgoods(); //13.查询所有的宿舍物品信息select_allgoods()
break;
case 14:
Goods_OP.add_good(); //14.添加宿舍物品信息的方法add_good()
break;
case 15:
Goods_OP.alter_good(); //15.更新指定的宿舍物品信息的方法alter_good()
break;
case 16:
Repair_OP.select_allrepair(); //16.查询所有宿舍的维修信息的方法select_allrepair()
break;
case 17:
Repair_OP.alter_repair(); //17.更新指定宿舍的维修信息的方法alter_repair()
break;
case 18:
Repair_OP.add_repair(); //18.添加指定宿舍的维修信息的方法的add_repair()
break;
case 19:
all_OP.select_all(); //19.统计各楼栋的实住人数及应住人数的方法select_all()
break;
case 20:
all_OP.select_all1(); //20.统计各楼栋宿舍的实住人数以及应住人数select_all1()
break;
case 21:
System.out.println("是否要退出系统(Y/N)?");
String string = sc.next();
if(string.equals("Y")||string.equals("y")) {
System.out.println("已经成功退出系统!!!");
return;
}else if (string.equals("N")||string.equals("n")) {
System.out.println("已取消!");
break;
}else {
System.out.println("输入错误自动退回菜单!");
showMenu.show();
break;
}
case 22:
showMenu.show();
break;
default:
System.out.println("输入编号错误!");
break;
}
}
}
}
Student_OP.java
package room_system;
import static room_system.JDBCUnit.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class Student_OP {
//1.查询所有学生的宿舍信息的方法select_allstudent()
public static void select_allstudent() {
String sql = "select * from Student order by Sno"; //要执行的SQL语句
ResultSet rs;
String Sno ;
String Sname ;
String Ssex ;
String Sdept;
String Bno;
String Rno;
String inputtime;
try{
Connection conn = getConnection();
Statement stmt =conn.createStatement();
rs = stmt.executeQuery(sql);
System.out.println("学号 "+","+"姓名 "+","+"性别"+","+"专业 "+","+"楼栋 "+","+"宿舍 "+","+"录入时间 ");
System.out.println();
while(rs.next()) {
Sno = rs.getString(1);
Sname = rs.getString(2);
Ssex = rs.getString(3);
Sdept = rs.getString(4);
Bno = rs.getString(5);
Rno =rs.getString(6);
inputtime =rs.getString(7);
System.out.println(Sno+","+Sname+","+ Ssex+","+Sdept+","+Bno+","+Rno+","+inputtime);
}
stmt.close();
rs.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//2.添加入住学生的宿舍信息的方法add_stu()
public static void add_stu() {
String sql ;
String s1;
String[] s2;
String Sno ;
String Sname ;
String Ssex ;
String Sdept ;
String Bno;
String Rno ;
String inputtime;
System.out.println("请按照Sno,Sname,Ssex,Sdept,Bno,Rno,inputtng的格式录入学生信息:");
Scanner sc = new Scanner(System.in);
s1 = sc.next();
s2= s1.split(",");
try
{
Sno = s2[0];
Sname = s2[1];
Ssex = s2[2];
Sdept = s2[3];
Bno = s2[4];
Rno =s2[5];
inputtime =s2[6];
if(select_flag(Sno)==true) { //判断Sno是否存在,true
System.out.println("录入失败:Sno已经存在!");
}
else {
String S_SQL = "insert into Student values(";
sql = S_SQL + "'" + Sno + "'" + "," + "'" + Sname + "'" + "," + "'" + Ssex + "'" + "," + "'"+ Sdept + "'"+ "," + "'" + Bno + "'"+","+"'" + Rno + "'"+","+"'"+inputtime+"'"+ ")";
stmt_DML(sql); //执行SQL语句
System.out.println("成功录入Sno为 " + Sno + "的学生!");
System.out.println();
}
}
catch (Exception e)
{
System.out.println("输入格式错误请重新输入!");
}
}
//3.更新入住学生的宿舍信息的方法alter_stu()
public static void alter_stu(){
String sql;
System.out.println("请输入要更新信息的学生Sno");
Scanner sc = new Scanner(System.in);
String Sno ;
Sno=sc.next();
while(true) { //判断Sno是否存在
if(select_flag(Sno)==false) {
System.out.println("您输入的Sno不存在,请重新输入");
while(true) {
try {
Sno=sc.next();//再输入
break;
} catch (Exception e) {
System.out.println("输入格式错误,请重新输入");
sc.nextLine();
}
}
}else {
break;
}
}
System.out.println("请输入要更改的信息格式为Sname,Rno,inputtime");
String s1 = sc.next(); //String s1;String []s2;s1=s1 = sc.next();s2= s1.split(",");
String[] s2 = s1.split(",");
String Sname = s2[0];
String Rno = s2[1];
String inputtime =s2[2];
sql = "update Student set Sname="+"'"+Sname+"'"+","+"Rno="+"'"+Rno+"'"+","+"inputtime="+"'"+inputtime+"'"+" where Sno="+"'"+Sno+"'";
stmt_DML(sql);
System.out.println("成功更新Sno为 "+Sno+"的学生信息!");
System.out.println();
}
//4.删除学生信息的方法delete_stu()
public static void delete_stu() {
String Sno;
System.out.println("请输入要删除信息的学生Sno");
Scanner s1 = new Scanner(System.in);
Sno = s1.next();
if (select_flag(Sno) == false) {
System.out.println("您输入的Sno不存在!");
}
else
{
String sql = "delete from Student where Sno =" + "'" + Sno + "'";
stmt_DML(sql);
System.out.println("成功删除Sno为 " + Sno + "的学生");
System.out.println();
}
}
//5.查询指定学生的宿舍信息的方法select_stu()
public static void select_stu() {
System.out.println("请输入要查询信息的学生Sno");
Scanner sc = new Scanner(System.in);
String Sno = sc.next();
if (select_flag(Sno) == false) { //false
System.out.println("您输入的Sno不存在!");
} else {
String sql = "select * from Student where Sno = " + "'" + Sno + "'";
ResultSet rs;
try {
Connection conn = getConnection();
Statement stmt =conn.createStatement();
rs = stmt.executeQuery(sql);
System.out.println("学号 "+","+"姓名 "+","+"性别"+","+"专业 "+","+"楼栋 "+","+"宿舍 "+","+"录入时间 ");
while (rs.next()) {
Sno = rs.getString(1);
String Sname = rs.getString(2);
String Ssex = rs.getString(3);
String Sdept = rs.getString(4);
String Bno = rs.getString(5);
String Rno = rs.getString(6);
String inputtime = rs.getString(7);
System.out.println(Sno+","+Sname+","+ Ssex+","+Sdept+","+Bno+","+Rno+","+inputtime);
System.out.println();
}
System.out.println();
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
后面的代码都是大同小异了,也不一一贴出来了,需要的后续可以发出来,这也算是比较基础的代码了。做得不好请大神指点一下,球球了
资源在此
点击下载
完