复习Java第一个项目学生信息管理系统 02(数据库及model层部分) &Java面试题final关键字作用和finally、finalize的区别&生活【记录一个咸鱼大学生三个月的奋进生活】014

本文详述了一位大学生在复习Java时构建的学生信息管理系统,包括数据库设计和model层的实现。数据库涉及学生、班级、菜单和用户权限等表。同时,文章也探讨了`final`关键字在Java面试中的作用以及与`finally`、`finalize`的区别。此外,作者还分享了健身和生活照片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复习Java(学生信息管理系统02数据库及model层部分)

数据库构建

之前做好了界面部分,今天就要根据该信息管理系统的功能设计数据库了
数据库使用sqlserver搭建,因为当时做这个项目的时候还是用sqlserver用的比较熟练,所以就用了sqlserver

1)学生信息表(students)
用于储存操作学生信息
内含字段:
student_id ——(学生学号)
student_name ——(学生姓名)
student_yuan ——(学生学院)
student_class ——(学生班级)
student_room ——(学生宿舍号)
student_sex ——(学生性别)
student_citysheng ——(学生所属省)
student_cityshi ——(学生所属市)
student_phone ——(学生电话号码)
student_home ——(学生家庭详细住址)

2)班级信息表(className)
用于储存操作班级信息,更多是关联students表以及更方便的对于已毕业班级的学生信息进行删除
内含字段:
student_class ——(班级名,可以作为关联学生信息表用)

3)菜单信息表(myMenu)
该数据库包含所有的功能菜单内容,用于动态挂靠显示窗体菜单信息,主要用于根据权限的动态挂靠菜单
内含字段:
menuId —— (菜单的Id,一级菜单是二位数,二级菜单是四位数)
menuName —— (菜单名,之后会显示在界面的菜单Jmenu中)
functionClass —— (动态加载的文件地址,点击菜单后会显示在界面中间操作区域的界面类的地址)

4)登录账号表(misUser)
该表包含所有可以登录进程序的账号,用于验证是否能登录,还根据roleId进行权限管理功能
内含字段:
userId —— (用户的账号,登录根据这个进行查询)
userName —— (用户的姓名,会在主页面进行欢迎显示)
userPass —— (用户的密码,登录时判断是否正确)
roleId —— (用户的操作权限类别,会连接auth表进行权限管理功能)

5)权限管理表(auth)
该表主要进行链接 菜单myMenu表的menuId 和 misUser表的roleId 进行权限分配
内含字段:
roleId —— (用户的操作权限类别,对应misUser表的roleId)
menuId—— (菜单的Id,这里存的都是二级操作菜单所以都是四位数,对应myMenu表的menuId)

model层的until连接数据库层

DBUtil类(进行有关数据库的操作):

package com.sm.framework.model.util;    // model层中的util部分

// 该项目采取MVC分层编写,本类是model层的DButil类,这里写所有项目中会用到的有关数据库的操作;  
// 1,连接数据库     2,更新数据     3,查询数据     4,关闭数据库相关对象

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DBUtil {
	private Connection conn = null;
	private Statement sta = null;
	private ResultSet rs =  null;
	
	// getDbConnection方法是活动连接数据库的conn方法,这样就不会定死一次连接,可以获得conn然后进行多次连接放进一个List中
	private Connection getDbConnection() {
		Connection myConn = null;
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");     // 这里连的是sqlserver
			// 大家要是连别的数据库可以看我这个帖子如何操作:(https://blog.youkuaiyun.com/coiokok/article/details/117788656)
			myConn = DriverManager.getConnection("jdbc:sqlserver:端口号;DatabaseName=xwl", "数据库账号", "数据库密码");
			System.out.println("连接成功");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return myConn;
	}
	
	
	// 这是在重写构造方法,当实例化DButil类时就调用连接数据库的getDbConnection方法获取连接,然后将获得的conn存进List之后再进行连接操作
	public DBUtil() {
		List<Connection> list = new ArrayList<Connection>();
		for(int i = 0; i < 1; i++) {         // i小于几就是连接几次,这个之后用的时候再更改,现在为了速度更快写成1
			list.add(this.getDbConnection());
		}
		this.conn = list.get(0);      // 然后拿出List里的第一个进行操作
	}
	

	// update更新数据库数据的方法,调用方法时传sql语句进来即可更新数据
	public int update(String sql) {
		int n = -1;
		try {
			this.sta = this.conn.createStatement();
			n = this.sta.executeUpdate(sql);
			if(n > 0) {
				System.out.println("更新成功");
			} else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
	
	
	// query实现数据库查询的方法,返回的是rs也就是ResultSet对象的东西,调用方法的时候获得rs再在调用类中完成输出
	public ResultSet query(String sql) {
		try {
			this.sta = this.conn.createStatement();
			this.rs = this.sta.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.rs;
	}
	
	
	// close是关闭数据库的方法
	public void close() {
		try {
			if(this.rs != null) {
				this.rs.close();
				this.rs = null;
			}
			if(this.sta != null) {
				this.sta.close();
				this.sta = null;
			}
			if(this.conn != null) {
				this.conn.close();
				this.conn = null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

DBUtilTest类(对数据库的操作进行测试看是否正常):

大家写完代码一定记得测试这里提供一个例子可以判断自己是否正常连接到数据库

package com.sm.framework.model.util;

// 该项目是model层的DButil类的测试类test,这里调用DButil类里的所有方法进行测试操作

import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtilTest {

	public static void main(String[] args) {
		
		// 实例化util类
		DBUtil dbUtil = new DBUtil();
		
		
		// 调用DBUtil里的更新方法完成的操作:
		// 1.新增
		String sql = "insert into students(student_id, student_name, student_yuan, student_class ,student_room, student_sex, student_citysheng, student_cityshi, student_phone, student_home)"
		+ " values('666666', '张三', '艺术学院', '艺术1801', '6-001', '男', '陕西省', '西安市', '16688886666', 'home')";
		dbUtil.update(sql);
		dbUtil.close();      // 调用关闭数据库方法
		
		
		// 2.删除
		String sql = "delete from students where student_id = '666666'";
		dbUtil.update(sql);
		dbUtil.close();      // 调用关闭数据库方法
		
		
		// 3.修改
		String sql = "update students set student_sex = '女' where student_name = '张三'";
		dbUtil.update(sql);
		dbUtil.close();      // 调用关闭数据库方法
		
		
		// 调用DBUtil里的查询方法完成操作:
		String sql = "select * from students";
		ResultSet rs = dbUtil.query(sql);
		try {
			while(rs.next()) {
				for(int i = 0 ; i < 10 ; i++) {
					System.out.print(rs.getString(i+1));
					System.out.print("  ");
				}
				System.out.println();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			dbUtil.close();      // 调用关闭数据库方法
		}
	}
}

model层的entity实体层

MisUser类(用户权限的实体类):
该类的名字一定要和数据库的表名一样,类中的属性要和数据库表里的字段名一样,里面所需要的每个方法都是可以右键快速创建的

package com.sm.framework.model.entity;

// 该项目采取MVC分层编写,本类是model层的entity的MisUser用户权限的实体类,用来对应misUser的用户权限数据库的类
// 之后会用到1.构造方法   2.get and set方法   3.toString()方法   4.hashCode() and equals()方法

public class MisUser {
	private String userId = null;
	private String userName = null;
	private String userPass = null;
	private String roleId = null;
	
	public MisUser() {
		// TODO Auto-generated constructor stub
	}
	
	// 构造方法,可以通过右键Source中的using Fields
	public MisUser(String userId, String userName, String userPass, String roleId) {
		this.userId = userId;
		this.userName = userName;
		this.userPass = userPass;
		this.roleId = roleId;
	}
	
	// 右键Source中的get and set
	public String getRoleId() {
		return roleId;
	}
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPass() {
		return userPass;
	}
	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}

	// 右键Source中的toString()方法
	@Override
	public String toString() {
		return "MisUser [userId=" + userId + ", userName=" + userName + ", userPass=" + userPass + ", roleId=" + roleId + "]";
	}

	// 右键Source中的hashCode() and equals()方法
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((roleId == null) ? 0 : roleId.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		result = prime * result + ((userName == null) ? 0 : userName.hashCode());
		result = prime * result + ((userPass == null) ? 0 : userPass.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MisUser other = (MisUser) obj;
		if (roleId == null) {
			if (other.roleId != null)
				return false;
		} else if (!roleId.equals(other.roleId))
			return false;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		if (userPass == null) {
			if (other.userPass != null)
				return false;
		} else if (!userPass.equals(other.userPass))
			return false;
		return true;
	}
}

className类(班级信息的实体类):
该类的名字一定要和数据库的表名一样,类中的属性要和数据库表里的字段名一样,里面所需要的每个方法都是可以右键快速创建的

package com.sm.framework.model.entity;

//该项目采取MVC分层编写,本类是model层的entity的students班级信息的实体类,用来对应数据库数据的类,之后的java代码都是通过这个实体类再去操作数据库
//之后会用到1.构造方法   2.get and set方法   3.toString()方法   4.hashCode() and equals()方法


public class className {
	
	private String student_class = null;       // 所有的变量名要和数据库表里的列名一样

	public className() {
		
	}
	
	public className(String student_class) {
		super();
		this.student_class = student_class;
	}

	public String getStudent_class() {
		return student_class;
	}

	public void setStudent_class(String student_class) {
		this.student_class = student_class;
	}

	@Override
	public String toString() {
		return "className [student_class=" + student_class + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((student_class == null) ? 0 : student_class.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		className other = (className) obj;
		if (student_class == null) {
			if (other.student_class != null)
				return false;
		} else if (!student_class.equals(other.student_class))
			return false;
		return true;
	}
}

students类(学生信息实体类):
该类的名字一定要和数据库的表名一样,类中的属性要和数据库表里的字段名一样,里面所需要的每个方法都是可以右键快速创建的

package com.sm.framework.model.entity;

// 该项目采取MVC分层编写,本类是model层的entity的students学生信息的实体类,用来对应数据库数据的类,之后的java代码都是通过这个实体类再去操作数据库
// 之后会用到1.构造方法   2.get and set方法   3.toString()方法   4.hashCode() and equals()方法

public class students {    // 这里的类名要和数据库所用的表名一样
	
	private String student_id = null;       // 所有的变量名要和数据库表里的列名一样
	private String student_name = null;
	private String student_yuan = null;
	private String student_class = null;
	private String student_room = null;
	private String student_sex = null;
	private String student_citysheng = null;
	private String student_cityshi = null;
	private String student_phone = null;
	private String student_home = null;
	
	
	public students() {
		// TODO Auto-generated constructor stub
	}
	
	
	// 构造方法,可以通过右键Source中的using Fields
	public students(String student_id, String student_name, String student_yuan, String student_class,
			String student_room, String student_sex, String student_citysheng, String student_cityshi,
			String student_phone, String student_home) {
		this.student_id = student_id;
		this.student_name = student_name;
		this.student_yuan = student_yuan;
		this.student_class = student_class;
		this.student_room = student_room;
		this.student_sex = student_sex;
		this.student_citysheng = student_citysheng;
		this.student_cityshi = student_cityshi;
		this.student_phone = student_phone;
		this.student_home = student_home;
	}
	

	// 右键Source中的get and set
	public String getStudent_id() {
		return student_id;
	}

	public void setStudent_id(String student_id) {
		this.student_id = student_id;
	}

	public String getStudent_name() {
		return student_name;
	}

	public void setStudent_name(String student_name) {
		this.student_name = student_name;
	}

	public String getStudent_yuan() {
		return student_yuan;
	}

	public void setStudent_yuan(String student_yuan) {
		this.student_yuan = student_yuan;
	}

	public String getStudent_class() {
		return student_class;
	}

	public void setStudent_class(String student_class) {
		this.student_class = student_class;
	}

	public String getStudent_room() {
		return student_room;
	}

	public void setStudent_room(String student_room) {
		this.student_room = student_room;
	}

	public String getStudent_sex() {
		return student_sex;
	}

	public void setStudent_sex(String student_sex) {
		this.student_sex = student_sex;
	}

	public String getStudent_citysheng() {
		return student_citysheng;
	}

	public void setStudent_citysheng(String student_citysheng) {
		this.student_citysheng = student_citysheng;
	}

	public String getStudent_cityshi() {
		return student_cityshi;
	}

	public void setStudent_cityshi(String student_cityshi) {
		this.student_cityshi = student_cityshi;
	}

	public String getStudent_phone() {
		return student_phone;
	}

	public void setStudent_phone(String student_phone) {
		this.student_phone = student_phone;
	}

	public String getStudent_home() {
		return student_home;
	}

	public void setStudent_home(String student_home) {
		this.student_home = student_home;
	}

	
	// 右键Source中的toString()方法
	@Override
	public String toString() {
		return "students [student_id=" + student_id + ", student_name=" + student_name + ", student_yuan="
				+ student_yuan + ", student_class=" + student_class + ", student_room=" + student_room
				+ ", student_sex=" + student_sex + ", student_citysheng=" + student_citysheng + ", student_cityshi="
				+ student_cityshi + ", student_phone=" + student_phone + ", student_home=" + student_home + "]";
	}

	
	// 右键Source中的hashCode() and equals()方法
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((student_citysheng == null) ? 0 : student_citysheng.hashCode());
		result = prime * result + ((student_cityshi == null) ? 0 : student_cityshi.hashCode());
		result = prime * result + ((student_class == null) ? 0 : student_class.hashCode());
		result = prime * result + ((student_home == null) ? 0 : student_home.hashCode());
		result = prime * result + ((student_id == null) ? 0 : student_id.hashCode());
		result = prime * result + ((student_name == null) ? 0 : student_name.hashCode());
		result = prime * result + ((student_phone == null) ? 0 : student_phone.hashCode());
		result = prime * result + ((student_room == null) ? 0 : student_room.hashCode());
		result = prime * result + ((student_sex == null) ? 0 : student_sex.hashCode());
		result = prime * result + ((student_yuan == null) ? 0 : student_yuan.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		students other = (students) obj;
		if (student_citysheng == null) {
			if (other.student_citysheng != null)
				return false;
		} else if (!student_citysheng.equals(other.student_citysheng))
			return false;
		if (student_cityshi == null) {
			if (other.student_cityshi != null)
				return false;
		} else if (!student_cityshi.equals(other.student_cityshi))
			return false;
		if (student_class == null) {
			if (other.student_class != null)
				return false;
		} else if (!student_class.equals(other.student_class))
			return false;
		if (student_home == null) {
			if (other.student_home != null)
				return false;
		} else if (!student_home.equals(other.student_home))
			return false;
		if (student_id == null) {
			if (other.student_id != null)
				return false;
		} else if (!student_id.equals(other.student_id))
			return false;
		if (student_name == null) {
			if (other.student_name != null)
				return false;
		} else if (!student_name.equals(other.student_name))
			return false;
		if (student_phone == null) {
			if (other.student_phone != null)
				return false;
		} else if (!student_phone.equals(other.student_phone))
			return false;
		if (student_room == null) {
			if (other.student_room != null)
				return false;
		} else if (!student_room.equals(other.student_room))
			return false;
		if (student_sex == null) {
			if (other.student_sex != null)
				return false;
		} else if (!student_sex.equals(other.student_sex))
			return false;
		if (student_yuan == null) {
			if (other.student_yuan != null)
				return false;
		} else if (!student_yuan.equals(other.student_yuan))
			return false;
		return true;
	}
}

MyMenu类(菜单信息实体类):
该类的名字一定要和数据库的表名一样,类中的属性要和数据库表里的字段名一样,里面所需要的每个方法都是可以右键快速创建的

package com.sm.framework.model.entity;

// 该项目采取MVC分层编写,本类是model层的entity的MyMenu动态菜单的实体类,用来对应myMenu数据库的类
// 之后会用到其中的get and set方法

public class MyMenu {
	private String menuId = null;
	private String menuName = null;
	private String functionClass = null;
	public String getMenuId() {
		return menuId;
	}
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String getfunctionClass() {
		return functionClass;
	}
	public void setfunctionClass(String functionClass) {
		this.functionClass = functionClass;
	}
}

学习Java面试题(final关键字的作用)

还是指路陈哈哈大佬的Java数据库相关面试题原帖,太牛啦!

今天面试题的内容我也在之前的帖子里有提过
可以去这里看一哈:
复习Java封装继承&Java异常抛出面试题&生活【记录一个咸鱼大学生三个月的奋进生活】004

学习Java面试题(final、finally、finalize的区别)

还是指路陈哈哈大佬的Java数据库相关面试题原帖,太牛啦!

健身

照片分享

作者:午夜三点半   作品名:城市金光  出自500px社区






2021.06.22  by wyh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aspiriln

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值