学生数据库管理系统(纯Java版本)

学生管理系统

 

数据库设计:

建两个表,一个学生信息表,一个用户表:

 

         学生信息表:student

        

        

        

         用户信息表:user

        

        

        

 

类的编写:

 

本项目采用MVC模式进行操作:

首先建立MVC三层模式,首先三个包,一个用户信息类包,一个功能数据处理包,一个功能操作包。

 

 主要操作流程:连接数据库操作:

设计JDBCUtiles类,实现数据库的连接操作:

package jdbc;



import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;



public class JDBCUtils {



         private static Connection conn;

         private static String url = "jdbc:mysql://127.0.0.1:3306/stu?useUnicode=true&characterEncoding=UTF-8";

         private static String user = "root";

         private static String password = "123456";

        

         //动态加载驱动

         static{

                  try {

                          Class.forName("com.mysql.jdbc.Driver.class");

                         

                  } catch (Exception e) {

                         

                  }

         }

        

         public static Connection getConn(){

                  if(conn == null)

                  {

                          try {

                                   conn = DriverManager.getConnection(url, user, password);       

                          } catch (SQLException e) {

                                   e.printStackTrace();

                          }

                  }

                  return conn;

         }

        

}

 

相应的功能模块操作:

查询全部学生信息的操作:

建立findAll()方法,返回一个带有用户信息信息的集合  

 /**

      * 查询所有学生

      */

     public List<Stu> findAll(){

          List<Stu> list = new ArrayList<Stu>();

          try {

               String sql = "select * from student";

               ps = conn.prepareStatement(sql);

               ResultSet rs = ps.executeQuery();

              

               while(rs.next()){

                    int id = rs.getInt("id");

                    String stuNo = rs.getString("stuNo");

                    String stuName = rs.getString("stuName");

                    String sex = rs.getString("sex");

                    int age = rs.getInt("age");

                    String grade = rs.getString("grade");

                   

                    //封装对象

                    Stu stu = new Stu(id, stuNo, stuName, sex, age, grade);

                    list.add(stu);

               }

          } catch (Exception e) {

               e.printStackTrace();

          }

          return list;

     }

 

添加信息的操作:

创建换一个add()方法,返回一个布尔类型的值,看信息添加是否成功

/**

      * 添加学生

      */

     public boolean add(Stu stu){

          try {

               String sql = "insert into student(stuNo,stuName,sex,age,grade) values(?,?,?,?,?)";

               ps = conn.prepareStatement(sql);

               ps.setString(1, stu.getStuNo());

               ps.setString(2, stu.getStuName());

               ps.setString(3, stu.getSex());

               ps.setInt(4, stu.getAge());

               ps.setString(5, stu.getGrade());

               boolean rel = ps.execute();

               if(rel){

                    return true;

               }

              

          } catch (Exception e) {

               e.printStackTrace();

          }

          return false;

     }

 

删除信息的操作:

创建delete(方法,返回一个布尔类型的值,看数据是否删除成功/

**

      * 删除学生

      */

     public boolean delete(String stuName){

          try {

               String sql = "delete from student where stuName=?";

               ps = conn.prepareStatement(sql);

               ps.setString(1, stuName);

              

               boolean rel = ps.execute();

               if(!rel){

                    return true;

               }

              

          } catch (Exception e) {

               e.printStackTrace();

          }

          return false;

     }

 

修改信息的操作:

创建updae()方法:返回一个布尔类型,看信息是否修改成功

/**

      * 修改学生

      */

     public boolean update(Stu stu){

          try {

               String sql = "update student set stuName=?,sex=?,age=?,grade=? where id=?";

               ps = conn.prepareStatement(sql);

               ps.setString(1, stu.getStuName());

               ps.setString(2, stu.getSex());

               ps.setInt(3, stu.getAge());

               ps.setString(4, stu.getGrade());

               ps.setInt(5, stu.getId());

              

               boolean rel = ps.execute();

               if(!rel){

                    return true;

               }

          } catch (Exception e) {

               e.printStackTrace();

          }

          return false;

     }

 

界面的设计:

采用视图的方式形成控制界面:

首先登陆界面:判断用户名跟密码是否正确,进入系统:

 

//用户登录方法

     public static void userLogin(){

          System.out.println("请输入用户名:");

          String name = sc.next();

          System.out.println("请输入密码:");

          String passwd = sc.next();

         

          //判断是否登录成功

          UserView userview = new UserView();

          boolean rel = userview.login(name, passwd);

         

          if(rel){

               new StuView().manager();

          }else{

               System.out.println("登录失败!");

          }

     }

    

     //用户登录

     public boolean login(String userName,String password){

         

          User user = us.login(userName, password);

          //判断是否登录成功

          if(user == null){

               return false;

          }

          return true;

     }

验证登陆成功后进入系统,执行相应的操作方式:

/**

      * 进入管理界面

      */

     public void manager(){

          out:

          while(true){

               System.out.println("*********欢迎使用学生管理系统**********");

               System.out.println("功能:1.添加学生;2.修改学生;3.删除学生;4.查询所有学生;0.退出登录;");

               int n = sc.nextInt();

               switch(n){

               case 1:

                    if(!addStu()){

                         System.out.println("添加数据成功!");

                    }else{

                         System.out.println("添加数据失败!");

                    }

                    break;

               case 2:

                    if(update()){

                         System.out.println("修改数据成功!");

                    }else{

                         System.out.println("修改数据失败!");

                    }

                    break;

               case 3:

                    if(delete()){

                         System.out.println("删除数据成功!");

                    }else{

                         System.out.println("删除数据失败!");

                    }

                    break;

               case 4:

                    findAll();

                    break;

               case 0:

                    break out;

               default:

                    System.out.println("输入有误!");

                    break;

               }

          }

     }

系统运行截图:

初次登录系统:选择2,进行用户注册

注册用户:

注册成功后登录:

登录成功执行相应的操作:

查询所有学生:

删除学生:

 

 

 

本次项目的亮点:

  1. 思维清除,逻辑合理,通俗易懂
  2. 采用MVC三层模式
  3. 实现了MySql数据库的基本操作,增删查改功能的实现

不足之处:

  1. 没有web界面,只有控制台操作
  2. MVC模式不完整,对相应的数据库的处理方法没有完成
  3. 功能相对简单,后期还需要完善
展开阅读全文

没有更多推荐了,返回首页