Java之学生信息管理系统升级版(数据库编程)

经过了几天的学习后,我觉得如果仅仅使用文件来保存数据,不仅十分繁琐而且数据也不安全,如果文件丢失或者出现异常,那么将无法挽回。这时候使用数据库MySQL来保存则更好。

学生类:

package Student;

public class Student {
    private String Name;
    private int StNo;
    private int StAge;
    private String Sex;
    public void SetStudent(String name,int StNo,int StAge,String sex){
        this.Name = name;
        this.StNo = StNo;
        this.StAge = StAge;
        this.Sex = sex;
    }
    public void print(){
        System.out.println("学号:"+this.StNo+" "+"姓名:"+this.Name+" "+"年龄:"+this.StAge);
    }
    public int GetNO()
    {
        return this.StNo;
    }
    public String GetName(){
        return this.Name;
        
    }
    public int GetAge(){
        return this.StAge;
    }
    public String GetSex(){
        return this.Sex;
    }
}
数据库类:

package Student;

import java.sql.*;
import java.util.Scanner;

public class StuSql {
    public boolean idIsUsed(Connection con, int id){
        Statement stat = null;
        try {
            stat = con.createStatement();
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        ResultSet result = null;
        try {
            result = stat.executeQuery("select * from test");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            while (result.next())
            {    
                if(id == result.getInt("id")){
                    return true;
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
    public Connection getconnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("成功加载MySql驱动器");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("找不到MySql驱动器");
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/hello";   //JDBC的URL,hello为数据库名字
        // MySQL配置时的用户名
        String user = "root"; 
        // MySQL配置时的密码
        String password = "123456";
        //调用DriverManager对象的getConnection()方法,获得一个Connection对象
        Connection con = null;
        try {
            con = DriverManager.getConnection(url,user,password);
            System.out.println("成功连接到数据库");
    }catch (SQLException e) {
        e.printStackTrace();
    }
        return con;    
    }
    public void insertStuSql(Connection con){
        Student st = new Student();
        System.out.println("请输入学号姓名年龄和性别:");
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        int StNo = sc.nextInt();
        String name = sc.next();
        int StAge = sc.nextInt();
        String sex = sc.next();
        st.SetStudent(name, StNo, StAge, sex);
        if (!idIsUsed(con, StNo) && StNo > 0) {
            java.sql.PreparedStatement stat = null;
            try {
                stat = con.prepareStatement("insert into test values(?, ?, ?, ?)");
            } catch (SQLException e4) {
                // TODO Auto-generated catch block
                e4.printStackTrace();
            }
            try {
                stat.setInt(1, st.GetNO());
            } catch (SQLException e3) {
                e3.printStackTrace();
            }
            try {
                stat.setString(2, st.GetName());
            } catch (SQLException e2) {
                e2.printStackTrace();
            }
            try {
                stat.setInt(3, st.GetAge());
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            try {
                stat.setString(4, st.GetSex());
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                stat.execute();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        else{
            System.out.println("无法添加,id已使用!");
        }
    }
    public void reviseStuSql(Connection con,int StuNo)
    {
        if (idIsUsed(con, StuNo) && StuNo > 0) {
            Statement stat = null;
            try {
                stat = con.createStatement();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String sql = "delete from test where id=" + StuNo;
            try {
                stat.executeUpdate(sql);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else{
            System.out.println("无法删除,用户不存在!");
        }
    }
    public void updateStuSql(Connection con,int StuNo){
        if (idIsUsed(con, StuNo) && StuNo > 0) {
            @SuppressWarnings("resource")
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入更新后的名字年龄性别:");
            String name = sc.next();
            int age = sc.nextInt();
            String sex = sc.next();
            String sql = "update test set name='" + name + "',age='"+age+"',sex='"+sex+"' where id='"
                    + StuNo + "'";
            PreparedStatement pstmt = null;
            try {
                pstmt = (PreparedStatement) con.prepareStatement(sql);
                int i = pstmt.executeUpdate();
                System.out.println("resutl: " + i);
                if (i < 1) {
                    System.out.println("用户不存在!");
                }
                pstmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else {
            System.out.println("无法更新,用户不存在!");
        }
    }
    public void selectStuSql(Connection con){
        Statement stat = null;
        try {
            stat = con.createStatement();
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        ResultSet result = null;
        try {
            result = stat.executeQuery("select * from test");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            System.out.println("当前数据如下:");
            while (result.next())
            {    
                System.out.println(result.getInt("id") + " " + result.getString("name") + " " + result.getInt("age")+" " + result.getString("sex"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
展示类:

package Student;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;

public class StList {
    public void Show(){
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        StuSql sql = new StuSql();
        Connection con = sql.getconnection();
        int value;
        while(true){
            System.out.println("学生管理系统");
            System.out.println("请输入相应的数字选择功能方式:");
            System.out.println("1:增加\t2:删除\t3:修改\t4:查看\t5:退出");
            value = scanner.nextInt();
            switch (value) {
            case 1:
                sql.insertStuSql(con);
                break;
            case 2:
                System.out.println("请输入要删除的学号:");
                int index = scanner.nextInt();
                sql.reviseStuSql(con, index);
                break;
            case 3:
                System.out.println("请输入要修改的学号!");
                int inde = scanner.nextInt();
                sql.updateStuSql(con, inde);
                break;
            case 4:
                sql.selectStuSql(con);
                break;
            case 5:
                System.out.println("程序已退出!");
                try {
                    con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return;
            default:
                System.out.println("您的输入有误!");
                break;
            }
        }
    }
    public static void main(String[] args){
        StList list = new StList();
        list.Show();
    }
}
 

猜你喜欢

转载自blog.csdn.net/pycharm_u/article/details/81174035