我把源代码和sql文件放GitHub上了,你们可以自行下载:https://github.com/fenglily1/student。
有问题可以留言或私信,我看到就会回。不要叫我大佬 我是菜
进阶版加上页面的管理系统在这篇:https://blog.csdn.net/weixin_44090966/article/details/91129395,不过是用JSP写的,有点老了,有兴趣的可以看看。
采用Java语言遍写一个简单的学生信息管理系统,通过JDBC连接数据库对学生信息进行增删改查,采用三层思想、DBUtils第三方框架和事务管理等;
包含以下功能:
dao层:
public class StudentDao {
//创建QueryRunner对象
private static QueryRunner qr = new QueryRunner(C3P0Utils.geDataSource());
//新增学生
public static void creatStudent(Student s) throws SQLException {
qr.update("insert into student values (null,?,?,?)",s.getSname(),s.getSage(),s.getSsex());
}
//删除学生
public static void deleteStudent(int sid) throws SQLException {
QueryRunner qr1 = new QueryRunner();
qr1.update(ConnectionManager.getConnection(),"delete from student where sid =?",sid);
}
//修改学生姓名
public static void updateSname(String sname,int sid) throws SQLException {
qr.update("update student set sname = ? where sid = ?",sname,sid);
}
//修改学生年龄
public static void updateSage(int sage,int sid) throws SQLException {
qr.update("update student set sage = ? where sid = ?",sage,sid);
}
//修改学生性别
public static void updateSsex(String ssex,int sid) throws SQLException {
qr.update("update student set ssex = ? where sid = ?",ssex,sid);
}
//查询学生信息
public static Student queryStudent(int sid) throws SQLException {
Student student = qr.query("select * from student where sid = ?",new BeanHandler(Student.class),sid);
return student;
}
//查询所有学生信息
public static List queryAllStudent() throws SQLException {
List list = qr.query("select * from student ",new BeanListHandler(Student.class));
return list;
}
}
service层:
public class StudentService {
//添加学生
public static void addStudent(Student s) {
try {
StudentDao.creatStudent(s);
} catch (SQLException e) {
System.out.println("添加学生失败");
e.printStackTrace();
}
}
//根据ID删除单个学生信息
public static void delete(int sid) {
try {
StudentDao.deleteStudent(sid);
} catch (SQLException e) {
System.out.println("删除学生失败");
e.printStackTrace();
}
}
//修改学生姓名
public static void updateSname(String sname, int sid) {
try {
StudentDao.updateSname(sname, sid);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("修改学生姓名失败");
}
}
//修改学生年龄
public static void updateSage(int sage, int sid) {
try {
StudentDao.updateSage(sage, sid);
} catch (SQLException e) {
System.out.println("修改学生年龄失败");
e.printStackTrace();
}
}
//修改学生性别
public static void updateSsex(String ssex, int sid) {
try {
StudentDao.updateSsex(ssex, sid);
} catch (SQLException e) {
System.out.println("修改学生性别失败");
e.printStackTrace();
}
}
//查询单个学生信息
public static Student queryStudent(int sid) {
Student student = null;
try {
student = StudentDao.queryStudent(sid);
} catch (SQLException e) {
System.out.println("查询学生信息失败");
e.printStackTrace();
}
return student;
}
//查询所有学生信息
public static List queryAllStudent() {
List list = null;
try {
list = StudentDao.queryAllStudent();
} catch (SQLException e) {
System.out.println("查询所有学生信息失败");
e.printStackTrace();
}
return list;
}
//批量删除学生信息
public static void deleteBatchStudent(List list) {
Connection connection = null;
//循环调用Dao的删除方法
try {
//开启事务
ConnectionManager.start();
for (int sid : list
) {
StudentDao.deleteStudent(sid);
//System.out.println(1/0);模拟出现问题
}
//提交事务
ConnectionManager.commit();
} catch (SQLException e) {
e.printStackTrace();
try {
//事务回滚
ConnectionManager.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally {
try {
ConnectionManager.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
view层
public class StudentView {
public static void main(String[] args){
System.out.println("欢迎来到学生信息管理系统~");
do {
managerStudent();
}while (true);
}
public static void managerStudent(){
//显示菜单
System.out.println("➡️请选择你要的功能:1.添加学生、2.删除单个学生、3.批量删除学生、4.修改学生信息、5.查询单个学生信息、6.查询所有学生信息、7.退出");
//获取用户输入的选择
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
//根据用户的选择实现相应的功能
switch (choice){
//添加学生信息
case 1:addStudent();break;
//删除单个学生
case 2:deleteStudent();break;
//批量删除学生
case 3:deleteBatchStudent();break;
//修改学生信息
case 4:updateStudent();break;
//查询单个学生
case 5:queryStudent();break;
//查询所有学生信息
case 6:queryAllStudent();break;
//退出
case 7:System.exit(0);break;
default:System.out.println("❌输入错误,请重新选择");break;
}
}
//查询单个学生
private static void queryStudent() {
System.out.println("请输入你要查询学生的学号:");
Scanner s = new Scanner(System.in);
int sid = s.nextInt();
Student student = StudentService.queryStudent(sid);
if(student == null){
System.out.println("查询失败,该学生不存在?");
}else {
System.out.println("?查询成功,以下为该学生信息:");
System.out.println(student);
}
}
//批量删除学生
private static void deleteBatchStudent() {
//创建一个集合,保存要删除学生的ID
List list = new LinkedList();
while (true){
System.out.println("请输入你要删除的学生学号(以-1结束):");
Scanner scanner = new Scanner(System.in);
int sid = scanner.nextInt();
if (sid == -1){
break;
}
//判断该学号学生是否存在
Student student = StudentService.queryStudent(sid);
if (student != null) {
list.add(sid);
System.out.println("已经标记该学生?");
System.out.println(student);
} else {
System.out.println("该学生不存在?");
}
}
System.out.println("您一个选择了"+list.size()+"个学生,是否确认全部删除?(y/n)");
Scanner scanner = new Scanner(System.in);
if("y".equals(scanner.nextLine())){
StudentService.deleteBatchStudent(list);
System.out.println("?批量删除"+list.size()+"个学生成功~");
}else {
System.out.println("取消批量删除操作");
}
}
//添加学生信息
public static void addStudent(){
System.out.println("请输入你要添加学生的姓名:");
Scanner scanner = new Scanner(System.in);
String sname = scanner.nextLine();
System.out.println("请输入你要添加学生的年龄:");
int sage = scanner.nextInt();
scanner.nextLine();//nextInt和netLine不能一起使用,用来接收"\n",不然没输入性别就自动提交了
System.out.println("请输入你要添加学生的性别:");
String ssex = scanner.nextLine();
//封装成学生对象
Student s = new Student(sname,sage,ssex);
//调用Service层的添加学生方法
StudentService.addStudent(s);
System.out.println("?添加学生"+sname+"成功");
}
//删除学生信息
public static void deleteStudent(){
System.out.println("请输入你要删除的学生学号:");
Scanner scanner = new Scanner(System.in);
int sid = scanner.nextInt();
Student student = StudentService.queryStudent(sid);
if(student == null){
System.out.println("?该学生不存在");
}else {
System.out.println("该学生信息如下:");
System.out.println(student);
scanner.nextLine();
System.out.println("⚠️确认删除?(y/n)");
String choice = scanner.nextLine();
if(choice.equals("y") || choice.equals("Y")){
StudentService.delete(sid);
System.out.println("删除学生"+sid+"成功");
}else {
System.out.println("取消删除...");
}
}
}
//修改学生信息
public static void updateStudent(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要修改学生的学号:");
int sid = scanner.nextInt();
Student student = StudentService.queryStudent(sid);
if(student == null){
System.out.println("?该学生不存在");
}else{
System.out.println("你要修改的学生信息如下:");
System.out.println(student);
int choice;
do {
System.out.println("请选择你要修改的学生信息:1.姓名、2.年龄、3.性别、4.退出修改");
choice= scanner.nextInt();
scanner.nextLine();
if (choice == 1) {
System.out.println("请输入你要修改的学生姓名:");
String sname = scanner.nextLine();
StudentService.updateSname(sname, sid);
System.out.println("修改成功?");
} else if (choice == 2) {
System.out.println("请输入你要修改的学生年龄:");
int sage = scanner.nextInt();
StudentService.updateSage(sage, sid);
System.out.println("修改成功?");
} else if(choice == 3){
System.out.println("请输入你要修改的学生性别:");
String ssex = scanner.nextLine();
StudentService.updateSsex(ssex, sid);
System.out.println("修改成功?");
}
}while (choice != 4);
System.out.println("你修改后该学生的信息如下:");
Student s = StudentService.queryStudent(sid);
System.out.println(s);
}
}
//查询所有学生信息
public static void queryAllStudent(){
List list = StudentService.queryAllStudent();
if(list.isEmpty()){
System.out.println("该数据库中暂无学生信息。");
}else {
for (Student s:list
) {
System.out.println(s);
}
System.out.println("所有学生信息显示完毕?");
}
}
}
domain包:
public class Student {
private int sid;
private String sname;
private int sage;
private String ssex;
public Student(int sid, String sname, int sage, String ssex) {
this.sid = sid;
this.sname = sname;
this.sage = sage;
this.ssex = ssex;
}
public Student() {
}
public Student(String sname, int sage, String ssex) {
this.sname = sname;
this.sage = sage;
this.ssex = ssex;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", sage=" + sage +
", ssex='" + ssex + '\'' +
'}';
}
}
utils包
C3P0Utils
public class C3P0Utils {
//创建C3P0连接池
private static ComboPooledDataSource ds = new ComboPooledDataSource();
//获取连接池对象
public static DataSource geDataSource(){
return ds;
}
//获取一个连接
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
ConnectionManager
//连接管理类,负责获取连接,开启事务,提交事务,回滚事务
public class ConnectionManager {
//定义一个ThreadLocal类对象来保存当前线程的连接
private static ThreadLocal tl = new ThreadLocal();
//获取连接
public static Connection getConnection() throws SQLException {
//先从tl中获取连接
Connection connection = tl.get();
//判断连接是否为空,
if (connection == null){
connection = C3P0Utils.getConnection();
tl.set(connection);
}
return connection;
}
//开启事务
public static void start() throws SQLException {
ConnectionManager.getConnection().setAutoCommit(false);
}
//提交事务
public static void commit() throws SQLException {
ConnectionManager.getConnection().commit();
}
//回滚事务
public static void rollback() throws SQLException {
ConnectionManager.getConnection().rollback();
}
//关闭连接
public static void close() throws SQLException {
ConnectionManager.getConnection().close();
}
}
项目截图: