数据库版的学生管理系统

我胡汉三又回来了. 回来更新我的数据库版的学生管理系统了…我自己可以说期待很久了, 很早就写完了. 不过也没有, 在上面进行写出来, 虽然很low 请大家多多建议建议…
说一下学生管理系统的业务逻辑
学生在此管理系统注册没有注册账号, 1.如果没有就请先注册账号,
1.1没有注册过的话,就先进行注册,并且账户名不可重复(这就要进行数据库的操作).
1.2如果注册过了,就请输入账户和密码, 1.2.1 如果输入正确 就请跳转到 学生管理系统里面. 1.2.2 如果没有输入正确就请重新输入 正确之后在进行跳转到学生关系系统
2. 在学生管理系统 , 进行数据库的增删改查的操作,
注意
仅仅适合初学者看,有助于理解 三层架构 , 理解 接口. 和实现类, 数据库与java之间的连接 ,
我这里写的很乱, 既有图片, 也有书写的代码, 主要是我刚刚知道怎么用这个软件,请原谅我的无知, 保证 ,之后再次更新图片就是图片, java代码就是java代码

这是我的包结构
在这里插入图片描述
在这我有 c3p0 连接池, druid (德鲁伊) 连接池(这是阿里旗下的产品, 国产的,灰常牛的一个连接池), jdbc连接池 在这里我使用的就是druid.properties ,
我使用的是三层架构; 数据访问层 (持久层) 业务层 表现层 (这里需要连接web 没有使用这个层) 直接在控制台进行打印的
配置文件
在这里插入图片描述
进行读取文件, 创建连接
在这里插入图片描述
user类,student 类
在这里插入图片描述
在这里插入图片描述
StudentSystem 打印在控制台的显示的代码

public class StudentSystem {
   private static Userservice userservice = new Userservice();
   private static Studentservice studentservice=new Studentservice();
   public static void main(String[] args) throws Exception {
       Scanner sc = new Scanner(System.in);
       ArrayList<Student> list = new ArrayList<>();
       login(sc);
       system(sc);

   }
   public static void login(Scanner sc) throws SQLException {

       System.out.println("=================学生信息系统管理登录页面====================");
       System.out.println("是否存在账号, y/n");
       String y = sc.next();
       while (true) {
           if (y.equalsIgnoreCase("y")) {
               System.out.println("请输入你的账号");
               String username = sc.next();
               System.out.println("请输入你的密码");
               String password = sc.next();
               boolean lohin = userservice.isLohin(new User(username, password));
               if (lohin) {
                   System.out.println("登录成功.......");
                   break;
               } else {
                   System.out.println("用户或密码错误");

               }
           }
           else  {
               while (true) {
                   System.out.println("=================学生信息系统管理注册页面====================");
                   System.out.println("请输入注册的账号");
                   String username = sc.next();
                   boolean user = userservice.isUsername(username);
                   if (user) {
                       System.out.println("你输入的用户已经存在,请重新输入");
                   } else {

                       System.out.println("请输入密码");
                       String password = sc.next();
                       userservice.isUser(new User(username, password));
                       System.out.println("你已经注册成功");
                       System.out.println("即将跳转到登录页面");
                       System.out.println("=================学生信息系统管理登录页面====================");
                       y="y";
                       break;
                   }
               }
           }
       }
   }

   private static void system(Scanner sc) throws Exception {
       while (true) {
           System.out.println("-------------------学生管理系统---------------------");
           System.out.println("1 添加 2 修改 3 删除 4 按学号查询 5 查询所有 6 退出 ");
           int num = sc.nextInt();
           switch (num) {
               case 1:
                   studentservice.addStudent(sc);
                   break;
               case 2:
                   studentservice.xiuStudent(sc);
                   break;
               case 3:
                   studentservice.deleteStudent(sc);
                   break;
               case 4:
                   studentservice.xueStudent(sc);
                   break;
               case 5:
                   studentservice.allStudent();
                   break;
               case 6:
                   System.out.println("即将退出系统!!");
                   System.exit(0);
                   break;
               default:
                   System.out.println("你输入有误请重新输入");
                   break;
           }
       }
   }

service层代码

//Student
public class Studentservice {
    private StudentDaoImp studentDaoImp=new StudentDaoImp();

   /* public ArrayList<Student> findall() throws SQLException {
        ArrayList<Student> all = studentDaoImp.findAll();
        return all;
    }*/
    public   void allStudent() throws SQLException {
        List<Student> list = studentDaoImp.findAll();
        System.out.println("_________________________________________");
        System.out.println("1 学号 \t\t 2 姓名 \t\t 3 年龄 \t\t 4 性别");
        System.out.println("=========================================");
        if (list == null || list.size() == 0) {
            System.out.println("警告;无数据");
        } else {
            for (Student stu : list) {
                System.out.println(stu.getStuNo()+"\t\t"+stu.getName()+"\t\t"+stu.getAge()+
                        "\t\t"+stu.getSex());
            }
        }
    }
    //增加
    public void addStudent(Scanner sc) throws SQLException {
        List<Student> list= studentDaoImp.findAll();
        int num ;
        a:
        while (true) {
            System.out.println("请输入你的学号");
            num = sc.nextInt();
            for (Student stu : list) {
                if (num == stu.getStuNo()) {
                    System.out.println("你输入的学号已经存在,重新输入");
                    continue a;
                }
            }
            break ;
        }
        System.out.println("请输入你的姓名");
        String name = sc.next();
        System.out.println("请输入你的年龄");
        int age = sc.nextInt();
        System.out.println("请输入你的性别");
        String sex = sc.next();
        Student stu = new Student(num,name,age,sex);
        studentDaoImp.insertStudent(stu);
        System.out.println("你已经录入成功");
    }
    //修改
   public void xiuStudent(Scanner sc) throws SQLException {
        List<Student> list =  studentDaoImp.findAll();
        System.out.println("请输入你要修改的学号");
        int num = sc.nextInt();
        for (Student stu : list) {
            if (num == stu.getStuNo()) {
                System.out.println("请输入你要修改的姓名,不修改输入0");
                String name = sc.next();
                System.out.println("请输入你要修改的年龄,不修改输入0");
                int age = sc.nextInt();
                System.out.println("请输入你要修改的性别,不修改输入0");
                String sex = sc.next();
                if (!name.equals("0")) {
                    stu.setName(name);
                }
                if (age != 0) {
                    stu.setAge(age);
                }
                if (!sex.equals("0")) {
                    stu.setSex(sex);
                }
                studentDaoImp.updateStudent(stu);
                System.out.println("修改完事");
                return;
            }
        }
        System.out.println("你学号输入错误不存在");
    }
    //删除
    public void deleteStudent(Scanner sc) throws SQLException {
        List<Student> list = studentDaoImp.findAll();
        System.out.println("输入你要删除的学号");
        int num = sc.nextInt();
        for (Student stu : list) {
            if (stu.getStuNo() == num) {
                System.out.println(stu);
                System.out.println("确认删除,y/n");
                String y = sc.next();
                if (y.equalsIgnoreCase("y")) {
                    studentDaoImp.deleteStudent(num);
                    System.out.println("已经删除");
                } else {
                    System.out.println("已退出此次操作");
                }
                return;
            }
        }
        System.out.println("你输入的学号不存在");
    }
    //按学号查询
    public void xueStudent(Scanner sc) throws SQLException {
        List<Student> list = studentDaoImp.findAll();
        System.out.println("请输入你要查询的学号");
        int num = sc.nextInt();
        for (Student stu : list) {
            if (num == stu.getStuNo()) {
                System.out.println(stu);
                return;
            }
        }
        System.out.println("你输入的学号不存在");
    }
}
//Userservice
public class Userservice {
    private Userdao usedao = new UserdaoImp();
    //验证用户是否存在
    public boolean isLohin(User user) throws SQLException {
        return usedao.isLogin(user);
    }
    public boolean isUsername(String username) throws SQLException {
       return usedao.isusername(username);
    }

    public int isUser(User user) throws SQLException {
        return usedao.isUser(user);
    }
}

dao 层

//UserDao
public interface Userdao {
    //找个接口进行实现方法
    public boolean isLogin(User user) throws SQLException;

    public boolean isusername(String username) throws SQLException;

    public int isUser(User user) throws SQLException;
}
//userdao的实现类
public class UserdaoImp implements Userdao {
     private JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
    @Override
    public boolean isLogin(User user) throws SQLException {
       //获取连接驱动
     //  Connection getconnection = JDBCUtils.getconnection();
        //编写sql语句
        String sql = "select * FROM user where username = ? and password =?";

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,
                user.getUsername(), user.getPassword());
        return  maps.size()>0 ?true:false;
        //高铁 ,
       //PreparedStatement preparedStatement = getconnection.prepareStatement(sql);
       //preparedStatement.setObject(1,user.getUsername());
       //preparedStatement.setObject(2,user.getPassword());
        //执行sql语句并返回结果
       //return preparedStatement.executeQuery().next();

        }

    @Override
    public boolean isusername(String username) throws SQLException {
        //获取连接驱动
      //  Connection getconnection = JDBCUtils.getconnection();
        //编写sql语句
        String sql = "SELECT * from USER  where username = ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, username);
        return maps.size()>0?true:false;
        //高铁 ,
      //  PreparedStatement preparedStatement = getconnection.prepareStatement(sql);
       // preparedStatement.setObject(1,username);

        //执行sql语句并返回结果
       // return preparedStatement.executeQuery().next();

    }

    @Override
    public int isUser(User user) throws SQLException {
        //获取连接驱动
     //   Connection getconnection = JDBCUtils.getconnection();
        //编写sql语句
        String sql = "insert into user (username,password) VALUES (?,?)";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, user.getUsername(), user.getPassword());
        return maps.size();
        //高铁 ,
     //   PreparedStatement preparedStatement = getconnection.prepareStatement(sql);
     //   preparedStatement.setObject(1,user.getUsername());
     //   preparedStatement.setObject(2,user.getPassword());
      //  return preparedStatement.executeUpdate();
    }

}
//StudentDao 
public interface Studentdao {
    //增加
    public  int insertStudent(Student student) throws SQLException;
    //修改
    public int updateStudent(Student student) throws SQLException;
    //删除
    public int deleteStudent(int stuNo) throws SQLException;
    //查看所有
    public List<Student> findAll() throws SQLException;
    //按id查询
    public Student findID(int stuNo) throws SQLException;
}
//StudentDao的实现类
public class StudentDaoImp implements Studentdao {
    private JdbcTemplate  jdbcTemplate =new JdbcTemplate(DataSourceUtils.getDataSource());
    @Override//增加
    public int insertStudent(Student student) throws SQLException {
        //获取数据库连接
       // Connection conn = JDBCUtils.getconnection();
        //Statement statement = conn.createStatement();

        String sql = "insert into student (id,name,age,sex) values(?,?,?,?)";
      //  int i = statement.executeUpdate(sql);
        //"+ student.getStuNo()+
        //                ",'"+student.getName()+"',"+student.getAge()+",'"+student.getSex()+"'
        int i = jdbcTemplate.update(sql, student.getStuNo(),student.getName(), student.getAge(), student.getSex());
        return i;
    }

    @Override//修改
    public int updateStudent(Student student) throws SQLException {
        //获取数据库
      //  Connection getconection = JDBCUtils.getconnection();
        //编写sql语句
        String sql = "update  student set name =?, age= ?,sex= ? where id = ?;";
        int i = jdbcTemplate.update(sql, student.getName(), student.getAge(), student.getSex(), student.getStuNo());
        //创建执行者
       // Statement statement = getconection.createStatement();
        //执行sql 并返回结果
       // int i = statement.executeUpdate(sql);
        return i;
    }

    @Override//删除
    public int deleteStudent(int stuNo) throws SQLException {
       // Connection getconnection = JDBCUtils.getconnection();
       // Statement statement = getconnection.createStatement();
        String sql = "delete from student where id = ?;";
        int i = jdbcTemplate.update(sql, stuNo);
        //int i = statement.executeUpdate(sql);
        return i;
    }

    @Override//查找所有
    public List<Student> findAll() throws SQLException {
       // ArrayList<Student> list = new ArrayList<>();
       //Connection getconnection = JDBCUtils.getconnection();
        //Statement statement = getconnection.createStatement();

        String sql = "select *  from  Student ";
        List<Student> list = jdbcTemplate.query(sql, new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                int id = resultSet.getByte("id");
                String name = resultSet.getString("name");
                int age = resultSet.getByte("age");
                String sex = resultSet.getString("sex");
                Student stu = new Student(id, name, age, sex);
                return stu;
            }
        });
        //ResultSet resultSet = statement.executeQuery(sql);
      /*  while (resultSet.next()) {
            int id = resultSet.getByte("id");
            String name = resultSet.getString("name");
            int age = resultSet.getByte("age");
            String sex = resultSet.getString("sex");
            list.add(new Student(id,name,age,sex));
        }*/
        return  list;
    }

    @Override//按学号查询
    public Student findID(int stuNo) throws SQLException {
        return null;
    }
}

之后会更新,web 敬请期待

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值