登录,注册

Posted 111wdh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了登录,注册相关的知识,希望对你有一定的参考价值。

 

页面

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();
    
}
}

 

以上是关于登录,注册的主要内容,如果未能解决你的问题,请参考以下文章

PHP注册与登录用户注册

片段无法转换为 java.util.concurrent.Executor 错误

如何制作 parse.com 注销按钮以将我带到另一个让我登录或注册的活动?

活动与片段实施

片段的视图模型

根据状态动态更改登录/注销按钮