JDBC登录的一些简单总结

该博客展示了从基础的命令行登录实现到使用DAO、自定义对象和业务逻辑封装的逐步优化过程,并最终实现了一个简单的Java Swing登录界面。通过这些步骤,作者演示了如何在登录功能中提高代码的可读性和复用性。
摘要由CSDN通过智能技术生成


准备:User类

package com.yue.model;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 鱼
 * @Date: 2021/05/23/12:14
 * @Description:
 */
public class User {
    private Integer userId;
    private String account;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", account='" + account + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

1.A_登录

package A_Frist;

import com.yue.util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 鱼
 * @Date: 2021/05/23/11:41
 * @Description:
 */
public class A_登录 {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("账号:");
        String account = scan.next();
        System.out.println("密码:");
        String password = scan.next();

        //连接数据库
        DBHelper dbHelper = new DBHelper();
        Connection conn = dbHelper.getConnection();
        String sql = "SELECT * FROM userinfo WHERE account=? and password=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,account);
        ps.setString(2,password);
        ResultSet rs = ps.executeQuery();

        if (rs.next()){
            System.out.println("登录成功");
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
            System.out.println(rs.getString(3));
        }else{
            System.out.println("登录失败");
        }

    }
}

2.B_登录

简化通道和封装

package A_Frist;

import com.yue.Dao.UserDao;
import com.yue.util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.Scanner;
public class B_登录 {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("账号:");
        String account = scan.next();
        System.out.println("密码:");
        String password = scan.next();

        //连接数据库
        UserDao userDao = new UserDao();
        String sql = "SELECT * FROM userinfo WHERE account=? and password=?";
        Map<String,Object> resultMap = userDao.queryForMap(sql,account,password);

        if (resultMap==null){
            System.out.println("登录失败");
        }else {
            System.out.println("登录成功");
            System.out.println(resultMap);
        }
    }
}

UserDao:

package com.yue.Dao;

import com.yue.model.User;
import com.yue.util.BaseDao;
public class UserDao extends BaseDao<User> {
}

3.C_登录

简化sql语句,封装为自定义对象

package A_Frist;

import com.yue.Dao.UserDao;
import com.yue.model.User;

import java.util.Map;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 鱼
 * @Date: 2021/05/23/11:41
 * @Description:
 */
public class C_登录 {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("账号:");
        String account = scan.next();
        System.out.println("密码:");
        String password = scan.next();

        //连接数据库
        UserDao userDao = new UserDao();
        User user = userDao.login(account,password);


        if (user==null){
            System.out.println("登录失败");
        }else {
            System.out.println("登录成功");
            System.out.println(user);
        }
    }
}

UserDao:

package com.yue.Dao;

import com.yue.model.User;
import com.yue.util.BaseDao;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 鱼
 * @Date: 2021/05/23/12:19
 * @Description:
 */
public class UserDao extends BaseDao<User> {
    public User login(String account,String password){
        String sql = "SELECT id userId,account,password FROM userinfo WHERE account=? and password=?";
        return  super.queryForRecordObject(sql,account,password);
    }
}

在这里插入图片描述

4.D_登录

简化业务逻辑判断

package A_Frist;


import com.yue.model.User;
import com.yue.service.UserService;


import java.util.Scanner;

public class D_登录 {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        System.out.println("账号:");
        String account = scan.next();
        System.out.println("密码:");
        String password = scan.next();

        //连接数据库
        UserService userService = new UserService();
        try {
            User user = userService.login(account,password);
            System.out.println("登陆成功");
            System.out.println(user);
        }catch (RuntimeException ex){
            System.out.println(ex.getMessage());
        }
    }
}

UserService

package com.yue.service;

import com.yue.Dao.UserDao;
import com.yue.model.User;


public class UserService {
    private UserDao userDao = new UserDao();
    public User login(String account,String password){
        User user = this.userDao.login(account,password);
        if (user==null){
            //利用抛出异常控制流程
            throw new RuntimeException("账号或密码错误,请重新输入!");
        }
        return user;
    }
}

5.c/s呈现

package A_Frist;

import com.yue.model.User;
import com.yue.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class E_登录界面 {
    public static void main(String[] args) throws Exception {
        new LoginView();
    }
}
class LoginView extends JFrame implements ActionListener{
    JButton loginButton;
    JButton exitButton;
    JTextField accountText;
    JPasswordField passwordText;
    public LoginView(){
        this.setTitle("用户登录窗口");
        this.setSize(250,300);
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        JLabel accountLabel = new JLabel("用户名");
        JLabel passwordLabel = new JLabel("密  码 ");
        loginButton = new JButton("登 录 ");

        //添加监听事件
        loginButton.addActionListener(this);
        exitButton = new JButton("退 出");
        exitButton.addActionListener(this);
        accountText = new JTextField(16);
        passwordText = new JPasswordField(16);


        //添加组件
        panel.add(accountLabel);
        panel.add(accountText);

        panel.add(passwordLabel);
        panel.add(passwordText);

        panel.add(loginButton);
        panel.add(exitButton);

        this.add(panel);
        this.setVisible(true);//显示窗口
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==loginButton){
            String account = accountText.getText();
            String password = passwordText.getText();
            //核心逻辑
            UserService userService = new UserService();
            try {
                User user = userService.login(account,password);
                JOptionPane.showMessageDialog(null,"登陆成功");
            }catch (RuntimeException ex){
                JOptionPane.showMessageDialog(null,ex.getMessage());
            }
        }else {
            System.exit(0);
        }
    }
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月色夜雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值