欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c/s实现增删改查 学生管理系统 Apache

程序员文章站 2022-05-08 14:43:51
...

**

使用Apache(阿帕奇)来实现c/s的增删改查

**

c/s实现增删改查 学生管理系统 Apache
需要导入的包:
commons-dbutils-1.7.jar
mysql-connector-java-5.1.32.jar

主界面代码

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

public class XStongji {
    static Scanner sc = new Scanner(System.in);
    static XSDao dao = new XSDao();

    public static void main(String[] args) throws Exception {

        while (true) {
            System.out.println("-----学生管理系统-----");
            System.out.println("-----1.学生查询全部-----");
            System.out.println("----2。学生关键字查询----");
            System.out.println("-----3.学生增加信息-----");
            System.out.println("-----4.学生修改信息-----");
            System.out.println("-----5.学生删除信息-----");
            System.out.println("-------0.退出系统-------");
            System.out.println("-----请输入要操作的数字:-----");
            int caozuo = sc.nextInt();
            if (caozuo == 1) {
                chaxunquanbu();
            } else if (caozuo == 2) {
                guanjianzichaxun();
            } else if (caozuo == 3) {
                zengjiaxinxi();
            } else if (caozuo == 4) {
                xiugaixinxi();
            } else if (caozuo == 5) {
                shanchuxinxi();
            } else if (caozuo == 0) {
                tuichuxit();
            }
        }
    }

    //查询全部
    private static void chaxunquanbu() throws Exception {
        List<XSlei> xSleiList = dao.chaxunqunbu();
        for (XSlei xSlei : xSleiList){
            System.out.println(xSlei);
        }
        //iter快捷键

    }

    //关键字查询
    private static void guanjianzichaxun() {
        System.out.println("请输入你想要的查询的关键字:");
        String guanjianzi = sc.next();
        List<XSlei> list = dao.gaunjianzi(guanjianzi);
        for (XSlei xSlei : list) {
            System.out.println(xSlei);
        }

    }

    //添加学生
    private static void zengjiaxinxi() {
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生性别:");
        String xingbie = sc.next();
        System.out.println("请输入学生年龄:");
        int nianling = sc.nextInt();
        XSlei xSlei = new XSlei(name,xingbie,nianling);
        int row = dao.tianjiaxuesheng(xSlei);
        if (row == 0){
            System.out.println("添加失败");
        }
        System.out.println("添加成功");
    }

    //修改学生信息
    private static void xiugaixinxi() {
        System.out.println("请输入要修改的学生id:");
        int xid = sc.nextInt();
        List<XSlei> w = dao.chaxunid(xid);
        for (XSlei xSlei : w) {
            System.out.println("要修改的学生信息如下:");
            System.out.println(xSlei);
        }
        System.out.println("修改后姓名:");
        String xname = sc.next();
        System.out.println("修改后性别:");
        String xxingbie = sc.next();
        System.out.println("修改后年龄:");
        int xnianling = sc.nextInt();
        XSlei xSlei = new XSlei(xid,xname,xxingbie,xnianling);
        int row = dao.xiugaixuesengxinxi(xSlei);
        if (row == 0){
            System.out.println("修改失败");
        }
        System.out.println("修改成功");
    }

    private static void shanchuxinxi() {
        System.out.println("请输入要删除的学生id:");
        int sid = sc.nextInt();
        List<XSlei> w = dao.chaxunid(sid);
        for (XSlei xSlei : w) {
            System.out.println("要修改的学生信息如下:");
            System.out.println(xSlei);
        }
        System.out.println("是否删除?");
        System.out.println("1 删除 0 不做改变");
        int ss = sc.nextInt();
        if (ss==1){
            int row = dao.shanchuxuesheng(sid);
            if (row == 0){
                System.out.println("删除失败");
            }else {
                System.out.println("删除成功");
            }

        }else {
            System.out.println("-不做改变-");
        }
    }

    private static void tuichuxit() {
        System.out.println("----退出学生管理系统----");
        System.exit(0);
    }

}

实体类代码:

public class XSlei {
    private int id;  //id
    private String name;  //姓名
    private String xingbie;  //性别
    private int nianling;  //年龄

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getXingbie() {
        return xingbie;
    }

    public void setXingbie(String xingbie) {
        this.xingbie = xingbie;
    }

    public int getNianling() {
        return nianling;
    }

    public void setNianling(int nianling) {
        this.nianling = nianling;
    }

    public XSlei(int id, String name, String xingbie, int nianling) {
        this.id = id;
        this.name = name;
        this.xingbie = xingbie;
        this.nianling = nianling;
    }

    public XSlei(String name, String xingbie, int nianling) {
        this.name = name;
        this.xingbie = xingbie;
        this.nianling = nianling;
    }

    public XSlei() {
    }

    @Override
    public String toString() {
        return "XSlei{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", xingbie='" + xingbie + '\'' +
                ", nianling=" + nianling +
                '}';
    }
}

DBUtil代码:
注意DBUtil URL要进行编码修改 不然添加进去就是问号
数据库名?后更改为:
useUnicode=true&characterEncoding=utf8&useSSL=false
同时数据库也要进行更改编码:
ALTER TABLE 表格名 CONVERT TO CHARACTER SET gbk COLLATE gbk_chinese_ci
c/s实现增删改查 学生管理系统 Apache

import java.sql.*;

//连接数据库
public class DBUtil {
    private static String URL = "jdbc:mysql://127.0.0.1/zuoye?useUnicode=true&characterEncoding=utf8&useSSL=false";    ////输入中文
    private static String driverName = "com.mysql.jdbc.Driver";
    private static String user = "root";    ////账号
    private static String password = "111111";     /////密码

    //获取连接
    public static Connection getConn() {
        Connection connection = null;
        try {
            Class.forName(driverName);
            connection = DriverManager.getConnection(URL,user,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //释放资源
    public static void closeAll(Connection conn, Statement stmt, ResultSet rs) {

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

Dao包代码:

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class XSDao {
    //查询全部
    public List<XSlei> chaxunqunbu() {
        ArrayList<XSlei> xSleiArrayList = new ArrayList<XSlei>();
        QueryRunner qr = new QueryRunner();
        String sql ="select * from xsguanli";
        try {
            xSleiArrayList = (ArrayList<XSlei>) qr.query(DBUtil.getConn(),sql,new BeanListHandler<XSlei>(XSlei.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return xSleiArrayList;

    }

    public List<XSlei> gaunjianzi(String guanjianzi) {
        ArrayList<XSlei> xSleiArrayList = new ArrayList<XSlei>();
        QueryRunner qr = new QueryRunner();
        String sql ="select * from xsguanli where name like ?";
        try {
            xSleiArrayList = (ArrayList<XSlei>) qr.query(DBUtil.getConn(),sql,new BeanListHandler<XSlei>(XSlei.class),"%"+guanjianzi+"%");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return xSleiArrayList;

    }

    //添加学生信息
    public int tianjiaxuesheng(XSlei xSlei) {
        int row = 0;
        QueryRunner qr = new QueryRunner();
        String sql ="insert into xsguanli values (null,?,?,?)";
        try {
            row = qr.update(DBUtil.getConn(),sql,xSlei.getName(),xSlei.getXingbie(),xSlei.getNianling());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return row;

    }

    public int xiugaixuesengxinxi(XSlei xSlei) {
        int row = 0;
        QueryRunner qr = new QueryRunner();
        String sql ="update xsguanli set name = ?, xingbie = ?, nianling = ? where id =?";
        try {
            row = qr.update(DBUtil.getConn(),sql,xSlei.getName(),xSlei.getXingbie(),xSlei.getNianling(),xSlei.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return row;
    }

    public int shanchuxuesheng(int sid) {
        int row = 0;
        QueryRunner qr = new QueryRunner();
        String sql ="delete from xsguanli where id =  ? ";
        try {
            row = qr.update(DBUtil.getConn(),sql,sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;


    }

    public ArrayList<XSlei> chaxunid(int xid) {
        ArrayList<XSlei> xSleiArrayList = new ArrayList<XSlei>();
        QueryRunner qr = new QueryRunner();
        String sql ="select * from xsguanli where id = ?";
        try {
            xSleiArrayList = (ArrayList<XSlei>) qr.query(DBUtil.getConn(),sql,new BeanListHandler<XSlei>(XSlei.class),xid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return xSleiArrayList;
    }
}


相关标签: mysql apache