效果如下:
数据库中用户表如下:
使用的数据库管理工具为 Navicat
(不知道Java怎么连接数据库的可看我的其他文章)
话不多说,直接上代码: 详细代码如下
import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.*;
public class Mysqlcode{
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/lbbrary?useSSL=false&serverTimezone=UTC";
private String name="root";
private String pwd="520000";
//函数进行MD5加密
public static String getMD5String(String str) {
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(str.getBytes());
// digest()最后确定返回md5 hash值,返回值为8位字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
//一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方)
return new BigInteger(1, md.digest()).toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//链接数据库验证密码
public void gotomysql( String uname,String upwd) {
Connection coon =null;
Statement stmt =null;
try {
Class.forName(JDBC_DRIVER);
System.out.println("链接数据库");
coon = DriverManager.getConnection(DB_URL,name,pwd);
System.out.println(" 实例化Statement对象...");
stmt = coon.createStatement();
String sql;
//根据用户名查找是否有符号的密码
sql = "SELECT UNAME,PASSWORD FROM users where UNAME='x'";
sql=sql.replaceFirst("x", uname);
System.out.println(sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
String username =rs.getNString("UNAME");
String PASSWD =rs.getNString("PASSWORD");
LoginStatue lo=new LoginStatue();
upwd=getMD5String(upwd);
System.out.println(upwd);
if(uname.equals(username)&&upwd.equals(PASSWD)) {
lo.loginsuccess();
lo.listerner();//点击退出结束程序
System.out.println(username);
System.out.println(PASSWD);
}
else
{
lo.loginbreakdown();
lo.listerner();//点击退出结束程序
System.out.print("密码错误");
}
}
rs.close();
stmt.close();
coon.close();
}
catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(coon!=null) coon.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
public void addinfo(String newname,String newpsw) {
Connection coon =null;
Statement stmt =null;
try {
Class.forName(JDBC_DRIVER);
System.out.println("链接数据库");
coon = DriverManager.getConnection(DB_URL,name,pwd);
System.out.println(" 实例化Statement对象...");
stmt = coon.createStatement();
String sql;
//md5加密
String password=getMD5String(newpsw);
//插入数据库
sql = "insert into users (UNAME,PASSWORD)"+"VALUES (?,?)";
try (PreparedStatement pStatement = coon.prepareStatement(sql)) {
pStatement.setString(1, newname);
pStatement.setString(2, password);
pStatement.execute();//执行
}
System.out.println("注册成功");
stmt.close();
coon.close();
}
catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(coon!=null) coon.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
登陆界面
代码如下
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class homepage {
private JFrame jFrame = new JFrame("登录");
private Container c = jFrame.getContentPane();
private JLabel a1 = new JLabel("用户名");
private JTextField username = new JTextField();
private JLabel a2 = new JLabel("密 码");
private JPasswordField password = new JPasswordField();
private JButton okbtn = new JButton("确定");
private JButton enrolled = new JButton("注册");
public void dome() {
//设置窗体的位置及大小
jFrame.setBounds(600, 200, 300, 220);
//设置一层相当于桌布的东西
c.setLayout(new BorderLayout());//布局管理器
//设置按下右上角X号后关闭
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//初始化--往窗体里放其他控件
init();
//设置窗体可见
jFrame.setVisible(true);
}
public void init() {
/*标题部分--North*/
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("mysql登录系统"));
c.add(titlePanel, "North");
/*输入部分--Center*/
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
a1.setBounds(50, 20, 50, 20);
a2.setBounds(50, 60, 50, 20);
fieldPanel.add(a1);
fieldPanel.add(a2);
username.setBounds(110, 20, 120, 20);
password.setBounds(110, 60, 120, 20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel, "Center");
/*按钮部分--South*/
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(okbtn);
buttonPanel.add(enrolled);
c.add(buttonPanel, "South");
}
public void listerner() {
//确认按下去获取
okbtn.addActionListener(new ActionListener() {
//@Override
Mysqlcode t1=new Mysqlcode();
public void actionPerformed(ActionEvent e) {
String uname = username.getText();
String upwd = String.valueOf(password.getPassword());
t1.gotomysql(uname,upwd);
}
});
//点击注册进行操作
enrolled.addActionListener(new ActionListener() {
//@Override
Enrolled enrolled=new Enrolled();
public void actionPerformed(ActionEvent e) {
enrolled.dome();
enrolled.listerner();
}
});
}
public static void main(String[] args) {
homepage mysql=new homepage();
mysql.dome();
mysql.listerner();
}
}
注册界面:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Enrolled {
//标题
private JFrame jFrame = new JFrame("注册");
private Container c = jFrame.getContentPane();
private JLabel a1 = new JLabel("用户名");
private JTextField username = new JTextField();
private JLabel a2 = new JLabel("密 码");
private JPasswordField password = new JPasswordField();
private JButton okbtn = new JButton("提交");
private JButton cancel = new JButton("取消");
public void dome() {
//设置窗体的位置及大小
jFrame.setBounds(600, 200, 300, 220);
//设置一层相当于桌布的东西
c.setLayout(new BorderLayout());//布局管理器
//设置按下右上角X号后关闭
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//初始化--往窗体里放其他控件
init();
//设置窗体可见
jFrame.setVisible(true);
}
public void init() {
/*标题部分--North*/
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
titlePanel.add(new JLabel("mysql用户注册"));
c.add(titlePanel, "North");
/*输入部分--Center*/
JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
a1.setBounds(50, 20, 50, 20);
a2.setBounds(50, 60, 50, 20);
fieldPanel.add(a1);
fieldPanel.add(a2);
username.setBounds(110, 20, 120, 20);
password.setBounds(110, 60, 120, 20);
fieldPanel.add(username);
fieldPanel.add(password);
c.add(fieldPanel, "Center");
/*按钮部分--South*/
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(okbtn);
buttonPanel.add(cancel);
c.add(buttonPanel, "South");
}
//点击反应
public void listerner() {
//确认按下去获取并发送数据到数据库
okbtn.addActionListener(new ActionListener() {
//@Override
Mysqlcode t1=new Mysqlcode();
public void actionPerformed(ActionEvent e) {
//打开一个登陆新页面
String newname = username.getText();
String newpwd = String.valueOf(password.getPassword());
t1.addinfo(newname, newpwd);
homepage home=new homepage();
home.dome();
home.listerner();
}
});
//点击取消进行操作
cancel.addActionListener(new ActionListener() {
//@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
最后的登陆提示框:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LoginStatue {
private JFrame jFrame = new JFrame("Error");
private Container c = jFrame.getContentPane();
private JButton out = new JButton("退出");
public void loginsuccess() {
jFrame.setBounds(300, 200, 300, 120);
//设置一层相当于桌布的东西
c.setLayout(new BorderLayout());//布局管理器
//设置按下右上角X号后关闭
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//初始化--往窗体里放其他控件
init(2);
//设置窗体可见
jFrame.setVisible(true);
}
public void loginbreakdown() {
//设置窗体的位置及大小
jFrame.setBounds(300, 200, 300, 120);
//设置一层相当于桌布的东西
c.setLayout(new BorderLayout());//布局管理器
//设置按下右上角X号后关闭
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//初始化--往窗体里放其他控件
init(1);
//设置窗体可见
jFrame.setVisible(true);
}
public void init( int x) {
/*标题部分--North*/
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new FlowLayout());
if(x==1) {
titlePanel.add(new JLabel("用户名或密码错误!"));}
if(x==2) {
titlePanel.add(new JLabel("登陆成功,欢迎你的到来!"));
}
c.add(titlePanel, "Center");
//按钮
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(out);
c.add(buttonPanel, "South");
}
public void listerner() {
//确认按下去获取
out.addActionListener(new ActionListener() {
//@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}