StudentManageSystem(学生管理系统)利用数据库实现

该博客展示了如何使用Java连接Mysql数据库,利用Druid连接池和Spring JDBC进行增删查改操作。详细步骤包括配置Druid.properties,创建数据库连接池,实现查找、删除、更新和添加学生的方法,并提供了主函数实现交互式操作。

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

目录

1,Mysql数据库连接池对象

2.加入druid.properties

3.查找的工具类

4,通过ID属性删除学生

5,更新

6.增加学生

7,主函数

1,Mysql数据库连接池对象

package dao;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {
    private static DataSource ds;

    static {
        Properties pro = new Properties();
        try {
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("dao/druid.properties"));
            ds= DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static DataSource getDataSource(){
        return ds;
    }

    public static void close(Statement stmt, Connection conn){
        try {
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet re,Statement stmt,Connection conn){
        try {
            re.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

2.加入druid.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ljh02
username=root
password=021012
initialSize=5
maxActive=10
maxWait=3000

 

3.查找的工具类

package CRUE;

import dao.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class ChaById {
    public static JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
    static Scanner sc = new Scanner(System.in);

    public static void selectById(){
        System.out.println("请输入要查找信息的学号");
        int id = sc.nextInt();
        String sql = "select *from student where id = "+id;
        List<Map<String, Object>> maps = jt.queryForList(sql);
        for (Map<String, Object> map : maps) {
            System.out.println(map);
        }
    }

    public static void selectByName(){
        System.out.println("请输入要查找信息的名字");
        String name = sc.next();
        String sql = "select * from student where name = '"+name+"'";
        List<Map<String, Object>> maps = jt.queryForList(sql);

        for (Map<String, Object> map : maps) {
            System.out.println(map);
        }
    }

    public static void selectAll(){
        System.out.println("查找全部信息");
        String sql = "select * from student";
        List<Map<String, Object>> maps = jt.queryForList(sql);
        for (Map<String, Object> map : maps) {
            System.out.println(map);
        }
    }
}

4,通过ID属性删除学生

package CRUE;

import dao.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Scanner;

public class ShanById {
    public static void delete(){
        Scanner sc = new Scanner(System.in);
        JdbcTemplate Jt = new JdbcTemplate(JDBCUtils.getDataSource());
        String sql = "delete from student where id = ?";
        System.out.printf("请输入需要删除同学的学号:");
        int id = sc.nextInt();
        int update = Jt.update(sql, id);
        if (update!=0) {
            System.out.println("删除成功");
        }
    }
}

5,更新

package CRUE;

import dao.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Update {
    public static JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());

    public static void UpdateByName(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号来确认人的名字");
        String str = sc.next();
        System.out.println("请输入新的名字");
        String name = sc.next();
        String sql = "Update student set name = '"+name+"' where id = '"+str+"'";
        int update = jt.update(sql);
        if(update!=0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }

    public static void UpdateByAge(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号来确认人的名字");
        String str = sc.next();
        System.out.println("请输入新的年龄");
        int age = sc.nextInt();
        String sql = "Update student set age = '"+age+"' where id = '"+str+"'";
        int update = jt.update(sql);
        if(update!=0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }


}

6.增加学生

package CRUE;

import dao.JDBCUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Scanner;

public class Zeng {
    public static void addd(){
        JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
        Scanner sc = new Scanner(System.in);
        String sql = "insert into student values (?,?,?,?,?,?,?)";
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入学号");
        int id = sc.nextInt();
        System.out.println("请输入性别");
        String grender = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入体重");
        double weight = sc.nextDouble();
        System.out.println("请输入身高");
        double hight = sc.nextDouble();
        System.out.println("请输入爱好(每个爱好拿英文逗号隔开)");
        String hobby = sc.next();
        int update = jt.update(sql, id,name,grender,age,weight,hight, hobby);
        if(update!=0){
            System.out.println("添加成功");
        }
    }
}

7,主函数

import CRUE.ChaById;
import CRUE.ShanById;
import CRUE.Update;
import CRUE.Zeng;

import java.util.Scanner;

/**
 * 利用数据库实现
 * 数据由数据库中导入,然后进行sql更改,返回值数据库
 */

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        pro();
        System.out.println("请输入功能序号");
        int num = sc.nextInt();
        while(num!=0){
            switch (num){
                case 1 :
                    Zeng.addd();
                    break;
                case 2:
                    ShanById.delete();
                    break;
                case 3:
                    swi();
                    break;
                case 4:
                    swi2();
                    break;
            }
            pro();
            System.out.println("请输入功能序号");
            num = sc.nextInt();
        }
    }

    public static void swi2(){
        Scanner sc = new Scanner(System.in);
        pro3();
        System.out.println("请输入查找的功能数字");
        int num = sc.nextInt();
        switch (num){
            case 1:
                Update.UpdateByName();
                break;
            case 2:
                Update.UpdateByAge();
                break;
        }
    }

    public static void pro3(){
        System.out.println("1,修改名字");
        System.out.println("2,修改年龄");
    }

    public static void swi(){
        Scanner sc = new Scanner(System.in);
        pro2();
        System.out.println("请输入查找的功能数字");
        int num = sc.nextInt();
        switch (num){
            case 1:
                ChaById.selectById();
                break;
            case 2:
                ChaById.selectByName();
                break;
            case 3:
                ChaById.selectAll();
                break;
        }
    }

    public static void pro(){
        System.out.println("功能");
        System.out.println("0,退出");
        System.out.println("1,增加信息");
        System.out.println("2,按照学号删除");
        System.out.println("3,查找");
        System.out.println("4,更改");
    }

    public static void pro2(){
        System.out.println("功能");
        System.out.println("1,利用学号查找");
        System.out.println("2,利用名字查找");
        System.out.println("3,查找全部信息");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值