• 登录,注册


    页面

    package com.oracle.view;
    
    import java.util.Scanner;
    
    import com.oracle.controller.UserController;
    
    public class UserView {
    //登录
        private UserController userController=new UserController();
        public void login(){
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入用户名:");
            String uname=sc.next();
            System.out.println("请输入密码:");
            String pwd=sc.next();
            //调用UserController的登录方法
            int row=userController.login(uname, pwd);
            if(row>0){
                System.out.println("登录成功");
                new SortView().menu();
            }else{
                System.out.println("用户名或密码错误");
            }
        }
        public void zhuce(){
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入姓名:");
            String uname=sc.next();
            System.out.println("请输入密码(三位数字):");
            String pwd=sc.next();
            String mima="[0-9]{3}";
            String name="[a-z]{3,8}";
            if(uname.matches(name)&&pwd.matches(mima)){
                int row=userController.chachong(uname, pwd);
                if(row>0){
                    System.out.println("注册成功");
                }else{
                    System.out.println("您已经注册过了");
                }
            }else{
                System.out.println("你输入的格式有误");
            }
            
            
        }
    }

    封装

    package com.oracle.controller;
    
    import com.oracle.domain.Sort;
    import com.oracle.service.UserService;
    
    public class UserController {
    private UserService userService=new UserService();
    //登录
    public int login(String uname,String pwd){
        return userService.login(uname, pwd);
    }
    
    public int chachong(String uname,String pwd){
        
        int row=userService.chachong(uname, pwd);
        return row;
    }
    
    }

    条件

    package com.oracle.service;
    
    import java.sql.SQLException;
    
    import com.oracle.dao.UserDao;
    import com.oracle.tools.JDBCUtils;
    
    public class UserService {
    private UserDao userDao=new UserDao();
    public int login(String uname,String pwd){
        int row=0;
        try {
            row=userDao.login(uname, pwd);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return row;
    }
    public int chachong(String uname,String pwd){
        int row=0;
        int rs=0;
        try {
            row=userDao.chachong(uname);
            if(row==0){
                rs=userDao.zhuce(uname, pwd);
                return rs;
            }else{
                return rs=0;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return rs=0;
        
    }
    
    }

    连接数据库

    package com.oracle.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import javax.swing.JButton;
    
    import com.oracle.tools.JDBCUtils;
    
    public class UserDao {
        //登录
    public int login(String uname,String pwd) throws SQLException{
        Connection conn=JDBCUtils.getConn();
        String sql="select count(*) from user where uname=? and pwd=?";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1, uname);
        pst.setString(2, pwd);
        ResultSet rs=pst.executeQuery();
        int row=0;
        while(rs.next()){
            row=rs.getInt(1);
        }
        return row;
    }
    public int chachong(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 row=0;
        while(rs.next()){
            row=rs.getInt(1);
        }
        return row;
        
    }
    public int zhuce(String uname,String pwd) throws SQLException{
        Connection conn=JDBCUtils.getConn();
        String sql="insert into user(uname,pwd) values(?,?)";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1, uname);
        pst.setString(2, pwd);
        int rs=pst.executeUpdate();
        JDBCUtils.close(conn, pst);
        return rs;
    }
    }

    测试

    package com.oracle.test;
    
    import com.oracle.view.MainView;
    
    public class Demo {
    public static void main(String[] args) {
        new MainView().mainMenu();
        
    }
    }
  • 相关阅读:
    Maven学习总结(九)——使用Nexus搭建Maven私服
    Maven学习总结(八)——使用Maven构建多模块项目
    Maven学习总结(七)——eclipse中使用Maven创建Web项目
    Maven学习总结(六)——Maven与Eclipse整合
    Maven学习总结(五)——聚合与继承
    Maven学习总结(四)——Maven核心概念
    剑指offer【12】- 二进制中1的个数
    剑指offer【11】- 矩形覆盖
    剑指offer【10】- 变态跳台阶
    剑指offer【09】- 跳台阶
  • 原文地址:https://www.cnblogs.com/111wdh/p/13350545.html
Copyright © 2020-2023  润新知