import com.roadjava.student.view.EnrolView;
import com.roadjava.entity.AdminDO;
import com.roadjava.service.AdminService;
import com.roadjava.service.impl.AdminServiceImpl;
import com.roadjava.student.view.LoginView;
import com.roadjava.student.view.MainView;
import com.roadjava.util.DBUtil;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class EnrolHandler extends KeyAdapter implements ActionListener {
private EnrolView enrolView;
public EnrolHandler(EnrolView enrolView){this.enrolView = enrolView;}
public void actionPerformed(ActionEvent e) {
JButton Jbutton = (JButton) e.getSource();
String text = Jbutton.getText();
if ("确定".equals(text)) {
sure();
}
}
private void sure() {
String username = enrolView.getUsernametext().getText();
char[] chars = enrolView.getPasswordtext().getPassword();
if (username == null || "".equals(username.trim()) || chars == null) {
JOptionPane.showMessageDialog(enrolView, "用户名密码必填");
return;
}
// 检查用户名是否已被占用
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConn();
String sql = "SELECT COUNT(*) FROM users WHERE user_name = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
rs = ps.executeQuery();
if (rs.next() && rs.getInt(1) > 0) {
JOptionPane.showMessageDialog(enrolView, "用户名已被占用!");
return;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DBUtil.closeRs(rs);
DBUtil.closePs(ps);
DBUtil.closeConn(conn);
}
insert();
}
private void insert(){
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConn();
String sql = "INSERT INTO users (`user_name`,`pwd`) values(?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, enrolView.getUsernametext().getText());
ps.setString(2, new String(enrolView.getPasswordtext().getPassword()));
int result = ps.executeUpdate();
if (result > 0) {
JOptionPane.showMessageDialog(enrolView, "注册成功!");
} else {
JOptionPane.showMessageDialog(enrolView, "注册失败!");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DBUtil.closePs(ps);
DBUtil.closeConn(conn);
}
}
}
package com.roadjava.student.view;
import com.roadjava.handler.EnrolHandler;
import com.roadjava.handler.LoginHandler;
import javax.swing.*;
import java.awt.*;
public class EnrolView extends JFrame {
JLabel username = new JLabel("用户名:");
JLabel password = new JLabel("密码:");
JTextField usernametext = new JTextField();
JPasswordField passwordtext = new JPasswordField();
JButton sure = new JButton("确定");
EnrolHandler enrolHandler;
public EnrolView() {
enrolHandler = new EnrolHandler(this);
setLayout(null);
username.setFont(new Font("华文行楷", Font.PLAIN, 15));
password.setFont(new Font("华文行楷", Font.PLAIN, 15));
Container c1 = getContentPane();
c1.add(username);
c1.add(password);
c1.add(usernametext);
c1.add(passwordtext);
c1.add(sure);
sure.addActionListener(enrolHandler);
sure.addKeyListener(enrolHandler);
username.setBounds(0, 0, 200, 200);
password.setBounds(0, 40, 200, 200);
usernametext.setBounds(60, 90, 100, 20);
passwordtext.setBounds(60, 130, 100, 20);
sure.setBounds(50, 200, 80, 50);
//设置默认按钮
getRootPane().setDefaultButton(sure);
setSize(200, 300);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new LoginView();
}
public JTextField getUsernametext() {
return usernametext;
}
public JPasswordField getPasswordtext() {
return passwordtext;
}
ackage com.roadjava.service.impl;
import com.roadjava.entity.AdminDO;
import com.roadjava.service.AdminService;
import com.roadjava.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class AdminServiceImpl implements AdminService {
public boolean validateAdmin(AdminDO adminDO) {
String userName = adminDO.getUserName();
String pwdParam = adminDO.getPwd();
String sql = "select pwd from user where user_name = ?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = DBUtil.getConn();
if (conn == null) {
return false;
}
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
resultSet = ps.executeQuery();
while (resultSet.next()) {
String pwd = resultSet.getString(1);
if (adminDO.getPwd().equals(pwd)) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeRs(resultSet);
DBUtil.closeConn(conn);
DBUtil.closePs(ps);
}
return false;
}
}
public class AdminServiceImpl implements AdminService {
public boolean validateAdmin(AdminDO adminDO) {
String userName = adminDO.getUserName();
String pwdParam = adminDO.getPwd();
String sql = "select pwd from user where user_name = ?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
conn = DBUtil.getConn();
if (conn == null) {
return false;
}
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
resultSet = ps.executeQuery();
while (resultSet.next()) {
String pwd = resultSet.getString(1);
if (adminDO.getPwd().equals(pwd)) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeRs(resultSet);
DBUtil.closeConn(conn);
DBUtil.closePs(ps);
}
return false;
}
}
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8&useSSl=false";
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String USER_NAME = "root";
private static final String PWD = "lpb666666";
static {
try {
//com.mysql.jdbc.Driver静态代码块
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getConn() {
try {
return DriverManager.getConnection(URL, USER_NAME, PWD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
public static void closeConn(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void closePs(PreparedStatement ps) {
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void closeRs(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}