• 分包


    view层作用: 视图层,即项目中的界面

     controller层作用: 控制层, 获取界面上的数据,为界面设置数据; 将要实现的功能交给业务层处理

     service层作用: 业务层, 功能的实现, controller控制层和数据访问层DAO交互, 将对数据库的操作交给DAO数据访问层来处理

    dao层作用: 数据访问层, 用来操作数据库表的数据

     db数据库: 这里指MySQL

     domain 实体包: 存放JavaBean

     tools工具包:存放项目中使用到的工具类

     test 测试包: 存放项目功能测试的代码

     

     以商店账户管理,货物管理功能为例简历的java项目

    dao层连接数据库数据库语句--------------》service层 功能的实现-----------------------》controller层---------------view层

    tools:装的是JDBCUtils

    package tools;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    
    public class JDBCUtils {
        public static Connection getConn() {
            // 1.注册驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // 2.获得连接对象
            String url = "jdbc:mysql://localhost:3306/smalmarket?characterEncoding=utf-8";
            String username = "root";
            String password = "123";
            Connection conn=null;
            try {
                conn = DriverManager.getConnection(url, username, password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return conn;
        }
        public static void close(Connection conn,PreparedStatement pst){
            if(pst!=null){
                try {
                    pst.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        public static void close(Connection conn,
                PreparedStatement pst,ResultSet rs){
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(pst!=null){
                try {
                    pst.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

     view层:

    package view;
    import java.util.List;
    import java.util.Scanner;
    import controller.SortController;
    import controller.UserController;
    import domain.Sort;
    public class MainView {
        private UserController userController=new UserController();
        private SortController sortController=new SortController();
        public void MainShow(){
            System.out.println("-----欢迎进入商城-----");
            System.out.println("1、登陆");
            System.out.println("2、注册");
            System.out.println("3、退出");
            System.out.println("4、请输入您的选择");    
        }
        public void IndexShow(){
            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("6、请输入您的选择");
        }
        //登陆
        public void login(){
            System.out.println("----登录页面----");
            System.out.println("请输入用户名");
            Scanner sc=new Scanner(System.in);
            String username=sc.next();
            System.out.println("请输入密码");
            String password=sc.next();
            //调用Controller层的登录方法
            int count=userController.login(username,password);
            if(count>0){
                System.out.println("登陆成功");
                run2();
            }else{
                System.out.println("登陆失败!");
            }
        }
        //一级菜单
        public void run(){
            Scanner sc=new Scanner(System.in);
            while(true){
                MainShow();
                int choose=sc.nextInt();
                switch(choose){
                case 1:
                    login();
                    break;
                case 2:register();
                    break;
                case 3:
                    System.out.println("退出成功");
                    return;
                }
            }
        }
        //二级菜单
        public void run2(){
            Scanner sc=new Scanner(System.in);
            while(true){
                IndexShow();
                int choose=sc.nextInt();
                switch(choose){
                case 1:addSort();
                    break;
                case 2:update();
                    break;
                case 3:delete();
                    break;
                case 4:getall();
                    break;
                case 5:
                    return;
                }
            }
        }
        //新增分类页面
        public void addSort(){
            Scanner sc=new Scanner(System.in);
            System.out.println("=====新增页面====");
            System.out.println("请输入你要新增的分类名称");
            String sname=sc.next();
            //调用controller新增分类的方法
            String mes=sortController.addSort(sname);
            System.out.println(mes);
            
        }
        //查询所有分类页面
        public void getall(){
            System.out.println("查询所有分类");
            //调用Controller调用方法
            List<Sort>list=sortController.getall();
            System.out.println("分类编号	分类名称");
            for(Sort s:list){
                System.out.println(s.getSid()+"	"+s.getSname());
            }
        }
        //修改分类页面
        public void update(){
            getall();
            System.out.println("请输入您要修改的分类编号");
            Scanner sc=new Scanner(System.in);
            int sid=sc.nextInt();
            System.out.println("请输入您要修改的分类名称");
            String sname=sc.next();
            //调用 controller方法
            String mes=sortController.updateSort(sid, sname);
            System.out.println(mes);
        }
        //删除元素
        public void delete(){
            getall();
            System.out.println("请输入您要删除的分类编号");
            Scanner sc=new Scanner(System.in);
            int sid=sc.nextInt();
            //调用controller方法
            String mes=sortController.deleteSort(sid);
            System.out.println(mes);
        }
        //注册
        public void register(){
            System.out.println("----注册页面----");
            System.out.println("请输入您要注册的用户名");
            Scanner sc=new Scanner(System.in);
            int count=1;
            String uname=null;
            
            while(count>0){
            uname=sc.next();
            //调用controller的验证用户名是否已存在
            count=userController.checkUsername(uname);
            if(count>0){
                System.out.println("用户已存在,请重新输入");
            }
            }
            System.out.println("请输入密码");
            String pwd=sc.next();
            //调用controller方法
            String mes=userController.register(uname,pwd);
            System.out.println(mes);
        }
    }

     

    package dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import domain.User;
    import tools.JDBCUtils;
    public class UserDao {
        //登录验证用户名或密码是否正确
        public  int login(String username,String password) throws SQLException{
            Connection conn=JDBCUtils.getConn();
            String sql="select count(*) from user where uname=? and pwd=?";
            PreparedStatement pst=conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, password);
            ResultSet rs=pst.executeQuery();
            int count=0;
            while(rs.next()){
                count=rs.getInt(1);
            }
            return count;
        }
        //验证用户名是否重复
    public int checkUsername(String uname) throws SQLException{
        Connection conn=JDBCUtils.getConn();
        String sql="select count(*) from user where uname=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1, uname);
        ResultSet rs=pst.executeQuery();
        //处理结果集
        int count=0;
            while(rs.next()){
                count=rs.getInt(1);
                }
        JDBCUtils.close(conn, pst,rs);
        return count;    
        }
    //注册
    public int register(User user) throws SQLException{
        Connection conn=JDBCUtils.getConn();//数据库连接语句
        String sql="insert into user(uname,pwd) values(?,?)";
        PreparedStatement pst=conn.prepareStatement(sql);//sql语句注入问题(1'or'1=1)
        pst.setString(1, user.getUname());//第一个问号,的值是user.getUname(),从1开始
        pst.setString(2, user.getPwd());
        int row = pst.executeUpdate();
        JDBCUtils.close(conn, pst);//释放资源,
        return row;    
        }
    }
    package dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import domain.User;
    import tools.JDBCUtils;
    public class UserDao {
        //登录验证用户名或密码是否正确
        public  int login(String username,String password) throws SQLException{
            Connection conn=JDBCUtils.getConn();
            String sql="select count(*) from user where uname=? and pwd=?";
            PreparedStatement pst=conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, password);
            ResultSet rs=pst.executeQuery();
            int count=0;
            while(rs.next()){
                count=rs.getInt(1);
            }
            return count;
        }
        //验证用户名是否重复
    public int checkUsername(String uname) throws SQLException{
        Connection conn=JDBCUtils.getConn();
        String sql="select count(*) from user where uname=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1, uname);
        ResultSet rs=pst.executeQuery();
        //处理结果集
        int count=0;
            while(rs.next()){
                count=rs.getInt(1);
                }
        JDBCUtils.close(conn, pst,rs);
        return count;    
        }
    //注册
    public int register(User user) throws SQLException{
        Connection conn=JDBCUtils.getConn();//数据库连接语句
        String sql="insert into user(uname,pwd) values(?,?)";
        PreparedStatement pst=conn.prepareStatement(sql);//sql语句注入问题(1'or'1=1)
        pst.setString(1, user.getUname());//第一个问号,的值是user.getUname(),从1开始
        pst.setString(2, user.getPwd());
        int row = pst.executeUpdate();
        JDBCUtils.close(conn, pst);//释放资源,
        return row;    
        }
    }
    package controller;
    
    import domain.User;
    import service.UserService;
    
    public class UserController {
        //1、封装前台数据传给后台
        //2、接收后台数据传给前台
        private UserService userService=new UserService();
        public int login(String username,String password){
            return userService.login(username, password);
        }
        //验证用户名是否存在
        public int checkUsername(String uname){
            return userService.checkUsername(uname);
        }
        //注册用户
        public String register(String uname,String pwd){
            //封装前台数据
            User user=new User ();
            user.setUname(uname);
            user.setPwd(pwd);
            return userService.register(user);
        }
    }
        
    package domain;
    
    public class User {
    private int uid;
    private String uname;
    private String pwd;
    public int getUid() {
        return uid;
    }
    public void setUid(int uid) {
        this.uid = uid;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    @Override
    public String toString() {
        return "User [uid=" + uid + ", uname=" + uname + ", pwd=" + pwd + "]";
    }
    
    }
    package dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import domain.Sort;
    import tools.JDBCUtils;
    public class SortDao {
        //新增分类
        public int addSort(String sname) throws SQLException{
            Connection conn=JDBCUtils.getConn();
            String sql="insert into sort(sname)values(?)";
            PreparedStatement pst=conn.prepareStatement(sql);
            pst.setString(1, sname);    
            int row = pst.executeUpdate();
            JDBCUtils.close(conn, pst);
            return row;    
        }
        //查询所有分类
        public List<Sort>getall() throws SQLException{
            Connection conn=JDBCUtils.getConn();
            String sql="select * from sort";
            PreparedStatement pst=conn.prepareStatement(sql);
            ResultSet rs=pst.executeQuery();
            //处理结果集
            List<Sort>list=new ArrayList<Sort>();
            while(rs.next()){
                Sort sort=new Sort();
                sort.setSid(rs.getInt("sid"));
                sort.setSname(rs.getString("sname"));
                list.add(sort);
            }
            //释放资源
            JDBCUtils.close(conn, pst, rs);
            return list;
        }
        //修改分类方法
        public int updateSort(int sid,String sname) throws SQLException{
            Connection conn=JDBCUtils.getConn();
            String sql="update sort set sname=? where sid=?";
            PreparedStatement pst=conn.prepareStatement(sql);
            pst.setString(1, sname);
            pst.setInt(2, sid);
            int row=pst.executeUpdate();
            JDBCUtils.close(conn, pst);
            return row;
        }
        //删除分类
        public int deleteSort(int sid) throws SQLException{
            Connection conn=JDBCUtils.getConn();
            String sql="delete from sort where sid=?";
            PreparedStatement pst=conn.prepareStatement(sql);
            pst.setInt(1, sid);
            int row=pst.executeUpdate();
            JDBCUtils.close(conn, pst);
            return row;    
        }
    }
    package service;
    import java.sql.SQLException;
    import java.util.List;
    import dao.SortDao;
    import domain.Sort;
    public class SortService {
        private SortDao sortDao=new SortDao();
        //新增分类
        public String addSort(String sname){
        int row=0;
        String mes="";
         try {
            row=sortDao.addSort(sname);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         if(row>0){
             mes="新增成功";
         }else{
             mes="新增失败";
         }
         return mes;
        }
        public List<Sort> getall(){
            List<Sort> list=null;
            try {
                list=sortDao.getall();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
        }
        public String updateSort(int sid,String sname){
            int row=0;
            String mes="";
            try {
                row=sortDao.updateSort(sid,sname);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(row>0){
                mes="修改成功";
            }else{
                mes="修改失败";
            }
            return mes;
        }
        //删除
        public String deleteSort(int sid){
            int row=0;
            String  mes="";
            try {
            row=sortDao.deleteSort(sid);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(row>0){
                mes="删除成功";
            }else{
                mes="删除失败";
            }
            return mes;
        }
        
    }
    package controller;
    import java.util.List;
    import domain.Sort;
    import service.SortService;
    public class SortController {
        private SortService sortService=new SortService();
        //新增
        public String addSort(String sname){
            return sortService.addSort(sname);
        }
        //查询
        public List<Sort> getall(){
            return sortService.getall();
        }
        //修改分类
        public String updateSort(int sid,String sname){
            return sortService.updateSort(sid,sname);
        }
        //删除分类
        public String deleteSort(int sid){
            return sortService.deleteSort(sid);
        }
    }
    package domain;
    
    public class Sort {
        private int sid;
        private String sname;
        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;
        }
        @Override
        public String toString() {
            return "Sort [sid=" + sid + ", sname=" + sname + "]";
        }
        
        
    }
    package test;
    import view.MainView;
    public class Demo {
        public static void main(String[] args) {
            new MainView().run();    
        }
    }

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    蹉跎之印
    [转]cypress EZ-USB 68013在WIN7 64位下驱动识别方法 && 64位WIN7中禁用驱动程序签名强制
    cnki搜索候选
    [转]和机器学习和计算机视觉相关的数学
    [链接]SPSS 19 最新版破解版及教程下载
    fvtool函数
    [转]html中submit和button的区别(总结)
    [转]SAR与SIGMA DELTA的区别
    LINKAXES matlab plot
    Labview 自动清空前面板显示控件
  • 原文地址:https://www.cnblogs.com/yang1182/p/9819794.html
Copyright © 2020-2023  润新知