1、JWT注册窗口
package bysj_wzc.cilent;
import java.awt.*;
import javax.swing.*;
import bysj_wzc.cilent.ClientReadAndPrint.OpenRegisterListen;
//注册类
public class Register {
JTextField textField = null;
JPasswordField pwdField = null;
ClientReadAndPrint.RegisterListen listener=null;
ClientReadAndPrint.OpenLoginListen openLgLt=null;
// 构造函数
public Register() {
init();
}
void init() {
JFrame jf = new JFrame("注册");
jf.setBounds(500, 250, 310, 210);
jf.setResizable(false); // 设置是否缩放
JPanel jp1 = new JPanel();
JLabel headJLabel = new JLabel("注册界面");
headJLabel.setFont(new Font(null, 0, 35)); // 设置文本的字体类型、样式 和 大小
jp1.add(headJLabel);
JPanel jp2 = new JPanel();
JLabel nameJLabel = new JLabel("用户名:");
textField = new JTextField(20);
JLabel pwdJLabel = new JLabel("密码: ");
pwdField = new JPasswordField(20);
JButton registerBtn = new JButton("注册");
JButton loginBtn = new JButton("有账号?登录");
jp2.add(nameJLabel);
jp2.add(textField);
jp2.add(pwdJLabel);
jp2.add(pwdField);
jp2.add(registerBtn);
jp2.add(loginBtn);
JPanel jp = new JPanel(new BorderLayout()); // BorderLayout布局
jp.add(jp1, BorderLayout.NORTH);
jp.add(jp2, BorderLayout.CENTER);
// 设置监控
listener = new ClientReadAndPrint().new RegisterListen(); // 新建监听类
listener.setJTextField(textField); // 调用PoliceListen类的方法
listener.setJPasswordField(pwdField);
listener.setJFrame(jf);
pwdField.addActionListener(listener); // 密码框添加监听
registerBtn.addActionListener(listener); // 按钮添加监听
// 设置打开登录监控
openLgLt = new ClientReadAndPrint().new OpenLoginListen(); // 新建监听类
openLgLt.setJFrame(jf);
loginBtn.addActionListener(openLgLt); // 按钮添加监听
jf.add(jp);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭图标作用
jf.setVisible(true); // 设置可见
}
}
2、监听类
/**********************注册监听(内部类)**********************/
class RegisterListen implements ActionListener{
JTextField textField;
JPasswordField pwdField;
JFrame registerJFrame; // 注册窗口
Login login = null;
public void setJTextField(JTextField textField) {
this.textField = textField;
}
public void setJPasswordField(JPasswordField pwdField) {
this.pwdField = pwdField;
}
public void setJFrame(JFrame jFrame) {
this.registerJFrame = jFrame;
}
public void actionPerformed(ActionEvent event) {
userName = textField.getText();
String userPwd = String.valueOf(pwdField.getPassword()); // getPassword方法获得char数组
//通过账号密码注册
int i = DBUtils.addData(userName, userPwd);
if(i == -1) {
JOptionPane.showMessageDialog(registerJFrame, "已有此账号,请勿重复注册!", "提示", JOptionPane.WARNING_MESSAGE);
return;
}else if(i == 0) {
JOptionPane.showMessageDialog(registerJFrame, "注册失败,请查验参数是否超过大小", "提示", JOptionPane.WARNING_MESSAGE);
return;
}else {
JOptionPane.showMessageDialog(registerJFrame, "添加成功!", "提示", JOptionPane.WARNING_MESSAGE);
}
try {
Thread.sleep(1500);
}catch(Exception e) {
e.printStackTrace();
}
login = new Login(); // 新建登录窗口
registerJFrame.setVisible(false); // 隐藏注册窗口
}
}
/**********************切换注册页面监听(内部类)**********************/
class OpenRegisterListen implements ActionListener{
JTextField textField;
JPasswordField pwdField;
JFrame loginJFrame; // 登录窗口
Register register = null;
public void setJTextField(JTextField textField) {
this.textField = textField;
}
public void setJPasswordField(JPasswordField pwdField) {
this.pwdField = pwdField;
}
public void setJFrame(JFrame jFrame) {
this.loginJFrame = jFrame;
}
public void actionPerformed(ActionEvent event) {
register = new Register(); // 新建注册窗口
loginJFrame.setVisible(false); // 隐藏登录窗口
}
}
3、数据库操作
package bysj_wzc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class DBUtils {
// 加载数据库驱动 com.mysql.jdbc.Driver
private static String dbdriver = "com.mysql.cj.jdbc.Driver"; //因为MySQL是8.0版本,所以需要加上cj,如果是5.0版本就不用
// 获取mysql连接地址
private static String dburl = "jdbc:mysql://127.0.0.1:3306/databases?&useSSL=false&serverTimezone=UTC";
//这里的&serverTimezone=UTC很重要,之前就是因为这个出错
// 数据名称
private static String username = "root";
// 数据库密码
private static String userpassword = "admin";
// 获取一个数据的连接
public static Connection conn = null;
// 获取连接的一个状态
//下面是一个例子,其中database1是数据库名,后面是一条查询语句
public static void main(String[] args) throws SQLException {
System.out.println("进入");
// List x = getDataByNameAndPsd("1", "1");
int x = addData("44", "44");
System.out.println(x);
}
/**
* 获取用户数据通过账号
* @param userName 账号
* @return
*/
public static List getDataByName(String userName) {
List data = getData("bysj",
"select * from user where user_name = ?", userName);
return data;
}
/**
* 获取用户数据通过账号和密码
* @param userName 账号
* @param password 密码
* @return
*/
public static List getDataByNameAndPsd(String userName, String password) {
List data = getData("bysj",
"select * from user where user_name = ? and password = ?", userName, password);
return data;
}
/**
* 获取用户数据通过账号和密码
* @param userName 账号
* @param password 密码
* @return
*/
public static int addData(String userName, String password) {
//先查询数据库是否已经有此账号
List data = getDataByName(userName);
if(data != null && data.size() > 0) {
return -1;// -1 代表着数据库已经有次账号,无法继续添加
}
/*执行插入*/
//获取用户主键
String id = UUID.randomUUID().toString().replaceAll("-", "");
//执行添加方法
int count = operateData("bysj",
"insert into user(id, user_name, password) values(?,?,?)", id, userName, password);
return count;
}
/**
* 获取数据库连接
*
* @param mydbName
* @return
*/
private static Connection getConn(String mydbName) {
Connection conn = null;
try {
Class.forName(dbdriver);
String myjdbcUrl = dburl.replace("databases", mydbName);
conn = DriverManager.getConnection(myjdbcUrl, username, userpassword);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据库连接
*
* @param rs
* @param ps
* @param conn
*/
private static void closeAll(ResultSet rs, PreparedStatement ps,
Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn == null)
return;
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 查表,返回行的列表,每个列表中包含列的列表。
*
* @param dbName
* @param sql
* @return
*/
public static List getData(String dbName, String sql, String ...param) {
Connection conn = getConn(dbName);
PreparedStatement ps = null;
List list = new ArrayList();
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
//如果有传参
if(param != null) {
for(int i = 0;i < param.length;i++) {
ps.setString(i+1, param[i]);
}
}
rs = ps.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnCount; ++i) {
Object o = rs.getObject(i) == null ? "" : rs.getObject(i);
list.add(o);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(rs, ps, conn);
}
return list;
}
/**
* 添加/修改数据
*
* @param dbName 数据库名称
* @param sql
* @return
*/
public static int operateData(String dbName, String sql, String ...param) {
Connection conn = getConn(dbName);
PreparedStatement ps = null;
int rs = 0;
try {
ps = conn.prepareStatement(sql);
//如果有传参
if(param != null) {
for(int i = 0;i < param.length;i++) {
ps.setString(i+1, param[i]);
}
}
rs = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return rs;
}
}