成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Java編程基礎(chǔ)35——學(xué)生管理系統(tǒng)

chaosx110 / 2252人閱讀

摘要:視圖層表示層數(shù)據(jù)傳遞給層實(shí)現(xiàn)接收用戶輸入并調(diào)用打印菜單獲取用戶輸入調(diào)用對(duì)應(yīng)方法歡迎來到學(xué)生管理系統(tǒng)添加用戶編輯用戶查詢用戶刪除用戶退出系統(tǒng)請(qǐng)輸入要操作的功能序號(hào)接收用戶的菜單選擇對(duì)選擇的菜單判

1.視圖層
package net.allidea.studentoa.ui;
import java.util.List;
import java.util.Scanner;
import net.allidea.studentoa.bean.Student;
import net.allidea.studentoa.biz.StudentService;

/*
 * 表示層
 * 數(shù)據(jù)傳遞給service層實(shí)現(xiàn)
 */
public class IndexPage {
    private StudentService service = new StudentService();
    
    //接收用戶輸入,并調(diào)用
    public void run() {
        //1. 打印菜單 2. 獲取用戶輸入 3. 調(diào)用對(duì)應(yīng)方法
        boolean flag = true;
        Scanner in = new Scanner(System.in);
        while (flag) {
            System.out.println("------------歡迎來到學(xué)生管理系統(tǒng)-----------");
            System.out.println("1.添加用戶 2.編輯用戶 3.查詢用戶 4.刪除用戶 5.退出系統(tǒng)");
            System.out.println("請(qǐng)輸入要操作的功能序號(hào)[1-5]:");
            //接收用戶的菜單選擇
            int op = in.nextInt();
            //對(duì)選擇的菜單判斷,調(diào)用不同的功能
            switch (op) {
            case 1:
                //選擇添加用戶,調(diào)用添加用戶的方法
                addStudent();
                break;
            case 2:
                //選擇修改用戶,調(diào)用修改用戶的方法
                editStudent();
                break;
            case 3:
                //選擇刪除用戶,調(diào)用刪除用戶的方法
                selectStudent();
                break;
            case 4:
                //選擇查看用戶,調(diào)用查看用戶的方法
                deleteStudent();
                break;
            case 5:
                System.out.println("再見!");
                flag = false;
                break;
            default:
                System.out.println("輸入有誤,請(qǐng)重新輸入!");
            }
        }
    }

    private void addStudent() {  
        /*
         * 1. 獲取用戶輸入,封裝到bean中。 
         * 2. 調(diào)用service的addStudent()方法完成添加功能
         */
        System.out.println("選擇的添加用戶功能,請(qǐng)輸入以下內(nèi)容");
        Scanner sc = new Scanner(System.in);
        
        System.out.print("請(qǐng)輸入姓名:");
        String sname = sc.next();
        
        System.out.print("請(qǐng)輸入密碼:");
        String pass = sc.next();
        
        System.out.print("請(qǐng)輸入成績(jī):");
        double results = sc.nextDouble();
        
        System.out.print("請(qǐng)輸入日期:格式XXXX-XX-XX");
        String createtime = sc.next();

        //將接收到的數(shù)據(jù),調(diào)用service層的方法,傳遞參數(shù)
        //將用戶輸入的所有參數(shù),封裝成Student對(duì)象
        Student s = new Student(0, sname, pass, results, createtime);
        service.addStudent(s);
        System.out.println("添加用戶成功!");
    }
    
    
    private void editStudent() {
        //調(diào)用查詢所有財(cái)務(wù)數(shù)據(jù)的功能,顯示出啦
        //看到所有的數(shù)據(jù),從中選擇一向,進(jìn)行修改
        selectAll();
        System.out.println("選擇的是編輯功能,請(qǐng)輸入");
        Scanner sc = new Scanner(System.in);
        System.out.print("請(qǐng)輸入ID:");
        int sid = sc.nextInt();
        
        System.out.print("請(qǐng)輸入姓名:");
        String sname = sc.next();
        
        System.out.print("請(qǐng)輸入密碼:");
        String pass = sc.next();
        
        System.out.print("請(qǐng)輸入成績(jī):");
        double results = sc.nextDouble();
        
        System.out.print("請(qǐng)輸入日期:格式XXXX-XX-XX");
        String createtime = sc.next();
        
        //將所有用戶輸入的數(shù)據(jù),封裝到Student對(duì)象
        Student s = new Student(sid, sname, pass, results, createtime);
        //調(diào)用controller層中的方法實(shí)現(xiàn)編輯用戶
        service.editStudent(s);
        System.out.println("編輯用戶成功!");
    }
    
    //定義刪除方法
    private void deleteStudent() {
        selectAll();
        System.out.println("選擇的是刪除功能,請(qǐng)輸入ID號(hào)即可");
        Scanner in = new Scanner(System.in);
        System.out.print("請(qǐng)輸入ID:");
        
        service.deleteStudent(in.nextInt());
        System.out.println("刪除用戶成功!");
    }
    
    /*
     * 定義方法selectStudent();
     * 顯示查詢方式, 1.所有 2.條件
     * 接收用戶的選擇 
     */
    private void selectStudent() {
         System.out.println("1. 查詢所有    2. 按時(shí)間查詢    3. 按成績(jī)查詢");
         Scanner sc = new Scanner(System.in);
         int selectChooser = sc.nextInt();
         //判斷根據(jù)用戶的選擇,調(diào)用不同的功能
         switch(selectChooser){
         case 1:
             //選擇的查詢所有,調(diào)用查詢所有的方法
             selectAll();
             break;
         case 2:
             //選的時(shí)間條件查詢,調(diào)用帶有查詢條件的方法
             selectDate();
             break;
         case 3:
             //選的成績(jī)條件查詢,調(diào)用帶有查詢條件的方法
             selectResults();
             break;     
         default:
                System.out.println("輸入錯(cuò)誤!");
            }     
         }
    
     /*
      * 定義方法,實(shí)現(xiàn)查詢所有的用戶數(shù)據(jù)
      */
    private void selectAll() {
        //調(diào)用控制層中的方法,查詢所有的用戶數(shù)據(jù)
        List list = service.selectAll();
        if(list.size() != 0)
            print(list);
        else
            System.out.println("沒有查詢到數(shù)據(jù)");
    }
    //輸出用戶數(shù)據(jù)方法,接收l(shuí)ist集合,遍歷集合,輸出表格
    private void print(List list) {
        //輸出表頭
        System.out.println("ID		姓名		密碼		成績(jī)		時(shí)間");
        //遍歷集合,輸出到控制臺(tái)
        
        for (Student s : list) {
            System.out.println(s.getSid() + "		" 
                    + s.getSname() + "		"
                    + s.getPass() + "		" 
                    + s.getResults() + "		"
                    + s.getCreatetime());
        }
    }
    
    /*
     * 定義方法,調(diào)用帶有查詢條件的方法
     * 提供用戶輸入日期,開始和結(jié)束日期
     * 調(diào)用service方法,傳遞2個(gè)日期的參數(shù),獲取到service查詢的結(jié)果集,打印出來
     */
    private void selectDate() {
        System.out.println("選擇條件查詢,輸入日期格式XXXX-XX-XX");
        Scanner in = new Scanner(System.in);
        System.out.print("請(qǐng)輸入起始時(shí)間:");
        String startDate = in.next();
        System.out.print("請(qǐng)輸入結(jié)束時(shí)間:");
        String endDate = in.next();
        
        //調(diào)用service層方法,傳遞日期,獲取查詢結(jié)果集
        List list = service.selectDate(startDate, endDate);
        if(list.size() != 0)
            print(list);
        else
            System.out.println("沒有查詢到數(shù)據(jù)");
    }
    
    
    private void selectResults() {
        System.out.println("選擇條件查詢,輸入成績(jī)格式XXXX-XX-XX");
        Scanner in = new Scanner(System.in);
        System.out.print("請(qǐng)輸入最小成績(jī):");
        double mixResults = in.nextDouble();
        System.out.print("請(qǐng)輸入最大成績(jī):");
        double maxResults = in.nextDouble();
        
        //調(diào)用service層方法,傳遞日期,獲取查詢結(jié)果集
        List list = service.selectResults(mixResults, maxResults);
        if(list.size() != 0)
            print(list);
        else
            System.out.println("沒有查詢到數(shù)據(jù)");
    }
}
2.業(yè)務(wù)層
package net.allidea.studentoa.biz;
import java.util.List;

import net.allidea.studentoa.bean.Student;
import net.allidea.studentoa.dao.StudentDao;
/*
 *  業(yè)務(wù)層類
 *  接收上一層的數(shù)據(jù)
 *  經(jīng)過計(jì)算,傳遞給dao層,操作數(shù)據(jù)庫(kù)
 *  調(diào)用dao層中的類,類成員位置,創(chuàng)建Dao類的對(duì)象
 */
public class StudentService {
    private StudentDao dao = new StudentDao();
    //定義方法,實(shí)現(xiàn)刪除用戶功能
    public void deleteStudent(int sid) {
        dao.deleteStudent(sid);
    }
    
    //定義方法,實(shí)現(xiàn)編輯用戶
    public void editStudent(Student s) {
        dao.editStudent(s);
    }
    /* 
     * 定義方法,添加數(shù)據(jù)功能
     * 有控制層調(diào)用,傳遞Student類型對(duì)象
     * 方法調(diào)用dao層方法,傳遞Student對(duì)象
     */
    public void addStudent(Student s) {
        dao.addStudent(s);
    }

    /*
     * 定義方法,實(shí)現(xiàn)條件查詢用戶
     * 方法由控制層調(diào)用,傳遞2個(gè)日期字符串
     * 調(diào)用dao層的方法,傳遞2個(gè)日期字符串
     * 獲取查詢結(jié)果集
     * 
     */
    public List selectDate(String startDate,String endDate) {
        return dao.selectDate(startDate, endDate);
    }
    
    /*
     * 定義方法,實(shí)現(xiàn)查詢所有用戶數(shù)據(jù)
     * 此方法由控制層調(diào)用,去調(diào)用dao層方法
     * 返回存儲(chǔ)Student對(duì)象的List集合
     */
    public List selectAll() {
        return dao.selectAll();
    }

    /*
     * 定義方法,實(shí)現(xiàn)條件查詢用戶
     * 方法由控制層調(diào)用,傳遞2個(gè)日期字符串
     * 調(diào)用dao層的方法,傳遞2個(gè)日期字符串
     * 獲取查詢結(jié)果集
     * 
     */
    public List selectResults(double mixResults,double maxResults) {
        return dao.selectResults(mixResults, maxResults);
    }
    

}
3.數(shù)據(jù)層
package net.allidea.studentoa.dao;
import java.sql.SQLException;
import java.util.List;

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

import net.allidea.studentoa.bean.Student;
import net.allidea.studentoa.common.JDBCUtils;

/*
 * 實(shí)現(xiàn)對(duì)數(shù)據(jù)表s_user數(shù)據(jù)增刪改差操作
 */
public class StudentDao {
    private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
    //定義方法,實(shí)現(xiàn)刪除用戶功能
    public void deleteStudent(int sid) {
        String sql = "delete from s_user where sid = ?";
        try {
            qr.update(sql, sid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    
    //定義方法,實(shí)現(xiàn)編輯用戶功能,更新數(shù)據(jù)表
    public void editStudent(Student s) {
        try {
        String sql = "update s_user set sname = ?,pass = ?,results = ?,createtime = ? where sid = ?" ;
        //創(chuàng)建對(duì)象數(shù)組,存儲(chǔ)所有實(shí)際參數(shù)
        Object[] params = {s.getSname(),s.getPass(),s.getResults(),s.getCreatetime(),s.getSid()};
        //調(diào)用qr對(duì)象中的方法,update執(zhí)行更新
        qr.update(sql, params);
        }catch(SQLException ex) {
            System.out.println(ex);
            throw new RuntimeException("用戶更新失敗");
        }
    }
    /* 
     * 定義方法,添加數(shù)據(jù)功能
     * 有業(yè)務(wù)層調(diào)用,傳遞Student類型對(duì)象
     * 將Student對(duì)象中的數(shù)據(jù),添加到數(shù)據(jù)表
     */
    public void addStudent(Student zw) {
        try {
            //拼寫sql語(yǔ)句
            String sql = "insert into s_user (sname,pass,results,createtime)values(?,?,?,?)";
            //創(chuàng)建對(duì)象數(shù)組,存儲(chǔ)5個(gè)占位符的實(shí)際參數(shù)
            //實(shí)際參數(shù)來說原始,傳遞過來的對(duì)象是Student
            Object[] params = {zw.getSname(),zw.getPass(),zw.getResults(),zw.getCreatetime()};
            //調(diào)用qr對(duì)象中的方法,update執(zhí)行添加
            qr.update(sql, params);
        }catch(SQLException ex) {
            System.out.println(ex);
            throw new RuntimeException("用戶添加失敗");
        }
        
    }
    
    
    /*
     * 定義方法,查詢數(shù)據(jù)庫(kù),帶有條件去查詢財(cái)務(wù)表
     * 由業(yè)務(wù)層調(diào)用,查詢結(jié)果集存儲(chǔ)到Bean對(duì)象,存儲(chǔ)到List集合
     * 調(diào)用者傳遞2個(gè)日期字符串
     * 
     */
    public List selectDate(String startDate,String endDate) {
        try {
            //拼寫條件查詢sql語(yǔ)句
            String sql = "select * from s_user where createtime between ? and ?";
            //定義對(duì)象數(shù)組,存儲(chǔ)?占位符
            Object[] params = {startDate,endDate};
            //調(diào)用qr對(duì)象的方法Query查詢數(shù)據(jù)表,獲取結(jié)果集
            return qr.query(sql, new BeanListHandler<>(Student.class),params);
        }catch(SQLException ex) {
            System.out.println(ex);
            throw new RuntimeException("條件查詢失敗");
        }
    }
    
    
    public List selectResults(double mixResults,double maxResults) {
        try {
            //拼寫條件查詢sql語(yǔ)句
            String sql = "select * from s_user where results between ? and ?";
            //定義對(duì)象數(shù)組,存儲(chǔ)?占位符s
            Object[] params = {mixResults,maxResults};
            //調(diào)用qr對(duì)象的方法Query查詢數(shù)據(jù)表,獲取結(jié)果集
            return qr.query(sql, new BeanListHandler<>(Student.class),params);
        }catch(SQLException ex) {
            System.out.println(ex);
            throw new RuntimeException("條件查詢失敗");
        }
    }
    
    /*
     * 定義方法,查詢數(shù)據(jù)庫(kù),獲取所有的用戶數(shù)據(jù)
     * 方法,由業(yè)務(wù)層調(diào)用
     * 結(jié)果集,將所有的用戶數(shù)據(jù),存儲(chǔ)到Bean對(duì)象中,存儲(chǔ)到集合中
     */
    public List selectAll() {
        try {
            //查詢用戶數(shù)據(jù)的SQL語(yǔ)句
            String sql = "select * from s_user";
            //調(diào)用qr對(duì)象的方法,query方法,結(jié)果集BeanListHandler
            List list = qr.query(sql, new BeanListHandler<>(Student.class));
            return list;
        }catch(SQLException ex) {
            System.out.println(ex);
            throw new RuntimeException("查詢所有用戶失敗");
        } 
    }
}
4.工具類
package net.allidea.studentoa.common;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
/*
 * 獲取數(shù)據(jù)庫(kù)連接的工具類
 * 實(shí)現(xiàn)連接池
 */
public class JDBCUtils {
    //創(chuàng)建BasicDatatSource對(duì)象
    private static BasicDataSource datasource = new BasicDataSource();
    //靜態(tài)代碼塊,實(shí)現(xiàn)必要的參數(shù)設(shè)置
    
    static {
        datasource.setDriverClassName("com.mysql.jdbc.Driver");
        datasource.setUrl("jdbc:mysql://localhost:3307/student");
        datasource.setUsername("root");
        datasource.setPassword("allidea");
        datasource.setMaxActive(10);
        datasource.setMaxIdle(5);
        datasource.setMinIdle(2);
        datasource.setInitialSize(10);
    }
    public static DataSource getDataSource() {
        return datasource;
    }
}
5.bean
package net.allidea.studentoa.bean;
public class Student {
    private int sid;
    private String sname;
    private String pass;
    private double results;
    private String createtime;
    public Student() {
        super();
        
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public double getResults() {
        return results;
    }

    public void setResults(double results) {
        this.results = results;
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public Student(int sid, String sname, String pass, double results, String createtime) {
        super();
        this.sid = sid;
        this.sname = sname;
        this.pass = pass;
        this.results = results;
        this.createtime = createtime;
    }

    @Override
    public String toString() {
        return "Student [sid=" + sid + ", sname=" + sname + ", pass=" + pass + ", results=" + results + ", createtime="
                + createtime + "]";
    }
}
6.主程序類
package net.allidea.studentoa.test;
import net.allidea.studentoa.ui.IndexPage;

/*
 * 主程序類,作用,開啟軟件程序即可
 */
public class MainTest {
    public static void main(String[] args) {
        new IndexPage().run();
    }
}
7.數(shù)據(jù)庫(kù)
-- 創(chuàng)建數(shù)據(jù)庫(kù):
create table s_user(
  sid int primary key auto_increment,
  sname varchar(20),
  pass varchar(50),
  results double,
  createtime date);

-- 添加列:
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (1,"張三",123456,"88","2018-5-02");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (2,"李逵",123456,"99","2018-5-23");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (3,"松江",123456,"56","2018-5-02");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (4,"林聰",123456,"95","2018-5-14");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (5,"茉莉",123456,"93","2018-6-02");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (6,"李陽(yáng)陽(yáng)",123456,"79","2018-5-02");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (7,"黃曉明",123456,"85","2018-7-8");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (8,"張小米",123456,"84","2018-8-02");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (9,"王大治",123456,"72","2018-8-7");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (10,"姚明",123456,"33","2018-8-5");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (11,"劉士奇",123456,"100","2018-8-02");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (12,"秦瓊",123456,"89","2018-9-02");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (13,"關(guān)羽",123456,"76","2018-9-1");
INSERT  INTO s_user(sid,sname,pass,results,createtime) VALUES (14,"曹操",123456,"78","2018-10-11");

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/110381.html

相關(guān)文章

  • Java編程基礎(chǔ)35——學(xué)生管理系統(tǒng)

    摘要:視圖層表示層數(shù)據(jù)傳遞給層實(shí)現(xiàn)接收用戶輸入并調(diào)用打印菜單獲取用戶輸入調(diào)用對(duì)應(yīng)方法歡迎來到學(xué)生管理系統(tǒng)添加用戶編輯用戶查詢用戶刪除用戶退出系統(tǒng)請(qǐng)輸入要操作的功能序號(hào)接收用戶的菜單選擇對(duì)選擇的菜單判 showImg(https://segmentfault.com/img/bVbiXjw?w=939&h=509); 1.視圖層 package net.allidea.studentoa.ui...

    mingde 評(píng)論0 收藏0
  • python初學(xué)——面向?qū)ο?em>編程作業(yè):學(xué)校選課系統(tǒng)

    摘要:如上海北京等管理員自己創(chuàng)建其他地方的學(xué)校管理員創(chuàng)建的課程。包含名字性別年齡等學(xué)員查詢可上課程學(xué)員選課,選擇學(xué)校課程,并付款。課程價(jià)格,周期課程價(jià)格,周期課程價(jià)格,周期創(chuàng)建講師屬于北京校區(qū),屬于上海校區(qū)。 作業(yè)需求 創(chuàng)建北京、上海 2 所學(xué)校 創(chuàng)建linux , python , go 3個(gè)課程,linuxpy在北京開, go 在上海開 課程包含,周期,價(jià)格,通過學(xué)校創(chuàng)建課程 通過學(xué)校...

    kbyyd24 評(píng)論0 收藏0
  • 在頭條干了兩年后含淚整理的職場(chǎng)經(jīng)驗(yàn),太真實(shí)....

    摘要:兩年半的時(shí)間完成了一次晉升,順利地從一個(gè)職場(chǎng)小菜鳥轉(zhuǎn)變?yōu)榱艘幻Y深打工人額。。后來隨著工作經(jīng)驗(yàn)的積累,我發(fā)現(xiàn)這個(gè)想法其實(shí)是非常錯(cuò)誤的。不可否認(rèn),互聯(lián)網(wǎng)行業(yè)在這一點(diǎn)上確實(shí)不如公務(wù)員等體制內(nèi)職業(yè)。后面的工作基本是在吃老本,沒有主動(dòng)學(xué)習(xí),。 先簡(jiǎn)單說一下背景,某不知名 985 的學(xué)生,18年畢業(yè)進(jìn)...

    不知名網(wǎng)友 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

chaosx110

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<