学生管理系统MYSQL数据库版

主方法

package com.zzu.main;

import com.zzu.tool.db.IRowMapper;
import com.zzu.tool.db.Linked;

import javax.swing.tree.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Main {
    private static Linked linked = new Linked();

    public static void main(String[] args) {
        System.out.println("*********************************");
        System.out.println("*\t\t\t\t*");
        System.out.println("*\t欢迎使用学生信息管理系统\t*");
        System.out.println("*\t\t\t\t*");
        System.out.println("*********************************");
        while (true) {
            menu();
        }
    }

    static void menu() {
        System.out.println("1、添加学生信息");
        System.out.println("2、删除学生信息");
        System.out.println("3、修改学生信息");//地址传递
        System.out.println("4、查询学生信息");//name
        System.out.println("请输入操作,以Enter键结束:");
        Scanner sc = new Scanner(System.in);
        int option  = sc.nextInt();
        switch (option) {
            case 1:
                System.out.println("请输入学号");
                String id = sc.next();
                String sql =  "select id from student where id='"+id+"'";
                if (linked.exist(sql)){
                    System.out.println("学号存在,请重试");
                    return;
                }
                System.out.println("请输入姓名");
                String name = sc.next();
                System.out.println("请输入电话号吗");
                String mobile = sc.next();
                System.out.println("请输入家庭地址");
                String address = sc.next();
                sql = "insert into student values('"+id+"','"+name+"','"+mobile+"','"+address+"')";
                if(linked.update(sql)){
                    System.out.println("添加成功");
                    return;
                }
                System.out.println("添加失败");
                break;
            case 2:
                System.out.println("请输入学号");
                id = sc.next();
                sql =  "select id from student where id='"+id+"'";
                if (!linked.exist(sql)){
                    System.out.println("学号不存在,删除失败");
                    return;
                }
                sql = "delete from student where id='"+id+"'";
                if(linked.update(sql)){
                    System.out.println("删除成功");
                    return;
                }
                System.out.println("删除失败");
                break;
            case 3:System.out.println("请输入学号");
                id = sc.next();
                sql =  "select id from student where id='"+id+"'";
                if (!linked.exist(sql)){
                    System.out.println("学号不存在,修改失败");
                    return;
                }
                System.out.println("请输入姓名");
                name = sc.next();
                System.out.println("请输入电话号吗");
                mobile = sc.next();
                System.out.println("请输入家庭地址");
                address = sc.next();
                sql = "update student set name = '"+name+"', mobile = '"+mobile+"',address = '"+address+"'";
                if(linked.update(sql)){
                    System.out.println("修改成功");
                    return;
                }
                System.out.println("修改失败");

                break;
            case 4:
                System.out.println("请输入学号");
                id = sc.next();
                sql =  "select id from student where id='"+id+"'";
                if (!linked.exist(sql)){
                    System.out.println("学号不存在,查询失败");
                    return;
                }
                class RowMappper implements IRowMapper{
                    @Override
                    public void rowMapper(ResultSet resultSet) {
                        while(true) {
                            try {
                                if (!resultSet.next()) break;
                                String name = resultSet.getString("name");
                                String mobile = resultSet.getString("mobile");
                                String address = resultSet.getString("address");
                                System.out.println(id + "," + name + "," + mobile + "," + address);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                IRowMapper rowMapper = new RowMappper();
                sql = "select name,mobile,address from student where id='"+id+"'";
                linked.select(sql,rowMapper);
                break;
            default:
                System.out.println("I'm Sorry,there is not the "+option+" option,please try again.");
        }
    }
}

工具类
抽象方法:

package com.zzu.tool.db;

import java.sql.ResultSet;

public interface IRowMapper {
    void rowMapper(ResultSet resultSet);
}

简化代码工具类:

package com.zzu.tool.db;

import java.sql.*;

public class Linked {
    //更新数据
    public static boolean update(String sql){
        Connection connection = null;
        Statement statement = null;
        try {
            //2.连接数据库
            connection = getConnection();
            statement = connection.createStatement();
            int i = statement.executeUpdate(sql);//执行SQL
            if(i>0){
                return true;
            }else return false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(statement,connection);
        }
        return false;
    }
    public static void login(String sql) {
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        try {
            /*Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/date_base?serverTimezone=UTC", "root", "root");*/
            connection = getConnection();
            System.out.println("连接成功");
            /*String sql = "select id,name,mobile,address from test";*/
            Statement statement = connection.createStatement();
            ResultSet re = statement.executeQuery(sql);
            if (re.next()) {
                String id = re.getString("id");
                String name = re.getString("name");
                String mobile = re.getString("mobile");
                String address = re.getString("address");
                System.out.println(id + "," + name + "," + mobile + "," + address);
            }
        } catch ( SQLException e) {
            e.printStackTrace();
        } finally {
            //7.释放资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            //避免空指针异常
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //查询封装,借助实现类
    public static void select(String sql, IRowMapper rowMapper) {//接口无法创建对象,所以rowMapper参数一定指向IRowMapper接口实现类对象
        Connection connection = null;
        Statement statement =null;
        ResultSet resultSet=null;
        try {
        /*    Class.forName("com.mysql.jdbc.Driver");//加载驱动
            String url = "jdbc:mysql://localhost:3306/date_base?serverTimezone=UTC";*/
            connection = getConnection();//DriverManager.getConnection(url, "root", "root");//获取连接
            statement = connection.createStatement();
            resultSet= statement.executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中
            rowMapper.rowMapper(resultSet);//因为rowMapper参数指向IRowMapper接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法  多态
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(resultSet,statement,connection);
        }
    }
    public static void close(ResultSet resultSet,Statement statement,Connection connection){
        try {
            if(resultSet!=null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(statement!=null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(connection!=null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Statement statement,Connection connection){
        try {
            if(statement!=null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(connection!=null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //连接
    public static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动
            String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
            return DriverManager.getConnection(url, "root", "root");//获取连接
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //判断是否存在
    public static boolean exist(String sql){
        Connection connection = null;
        Statement statement =null;
        ResultSet resultSet=null;
        try{
            connection = getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            return  resultSet.next();

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(resultSet,statement,connection);
        }
        return false;
    }
}

你可能感兴趣的:(学生管理系统MYSQL数据库版)