准备: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);
}
}
}