需求:希望代码能够实现登录注册逻辑
已经拥有的:
-
数据库里有一张user表,表里字段有
id(序号)
userName(用户名)
password(密码)
power(权限)
类型分别为
int
varchar
varchar
varchar
其中id自动递增,不能为空,且为主键
在user表里,已经存在一条记录,是老板的用户名和密码,这意味着接下来注册的时候往数据库里增添记录时power这个字段只能为“员工”。 -
已经写好的一个登录界面,放在Login.java里,代码如下:
package com.test.ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class Login extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField userName;
private JPasswordField pwd;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login() {
setTitle("挂号管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 566, 394);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("挂号管理系统登录");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 32));
lblNewLabel.setBounds(161, 58, 273, 42);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("用户名:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 24));
lblNewLabel_1.setBounds(68, 146, 96, 42);
contentPane.add(lblNewLabel_1);
userName = new JTextField();
userName.setBounds(161, 146, 235, 35);
contentPane.add(userName);
userName.setColumns(10);
pwd = new JPasswordField();
pwd.setBounds(161, 220, 235, 35);
contentPane.add(pwd);
JLabel lblNewLabel_1_1 = new JLabel("密码:");
lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 24));
lblNewLabel_1_1.setBounds(93, 213, 96, 42);
contentPane.add(lblNewLabel_1_1);
JButton btnNewButton = new JButton("登录");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { //实行登录的事件
//获取表单的值
}
});
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
btnNewButton.setBounds(161, 295, 93, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("注册");
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
btnNewButton_1.setBounds(301, 295, 93, 23);
contentPane.add(btnNewButton_1);
JComboBox comboBox = new JComboBox();
comboBox.setFont(new Font("宋体", Font.BOLD, 25));
comboBox.setModel(new DefaultComboBoxModel(new String[] {"老板", "员工"}));
comboBox.setBounds(443, 188, 99, 35);
contentPane.add(comboBox);
}
}
这个界面有一个下拉框,可以选择是老板登录还是员工登录
- 还有一个注册界面,代码放在Register.java里,代码如下:
package com.test.ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class Register extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField userName;
private JPasswordField pwd;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setTitle("挂号管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 566, 394);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("挂号管理系统登录");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 32));
lblNewLabel.setBounds(161, 58, 273, 42);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("用户名:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 24));
lblNewLabel_1.setBounds(68, 146, 96, 42);
contentPane.add(lblNewLabel_1);
userName = new JTextField();
userName.setBounds(161, 146, 235, 35);
contentPane.add(userName);
userName.setColumns(10);
pwd = new JPasswordField();
pwd.setBounds(161, 220, 235, 35);
contentPane.add(pwd);
JLabel lblNewLabel_1_1 = new JLabel("密码:");
lblNewLabel_1_1.setFont(new Font("宋体", Font.PLAIN, 24));
lblNewLabel_1_1.setBounds(93, 213, 96, 42);
contentPane.add(lblNewLabel_1_1);
JButton btnNewButton_1 = new JButton("注册");
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
btnNewButton_1.setBounds(233, 295, 93, 23);
contentPane.add(btnNewButton_1);
}
}
- 我还有一个写好的数据库连接文件,放在DBConnection.java里,你可能会用到,代码如下:
package com.test.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection {
//1.加载jdbc连接mysql的驱动
public final static String driver = "com.mysql.cj.jdbc.Driver";
//2.连接mysql数据库的地址
public final static String url="jdbc:mysql://localhost:3316/java107";//java107是库的名称
//3.连接mysql的用户名
public final static String user = "root";
//4.链接mysql的密码
public final static String pwd = "";
//static静态代码块加载jdbc的驱动
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//连接mysql的连接对象
public static Connection getConn() {
try {
return DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return null;
}
//关闭连接
public static void close(ResultSet rs,PreparedStatement ps,Connection conn) {
try {
if(rs !=null) {
rs.close();
}
if(ps !=null) {
ps.close();
}
if(conn !=null) {
conn.close();
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//验证jdbc类的使用
public static void main(String[] args) {
System.out.println(getConn());
}
}
- 我也已经写好了一个User类代码,放在com.test.model包里,代码如下
package com.test.model;
public class User {
public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
private int id;
private String userName;
private String password;
private String power;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPower() {
return power;
}
public void setPower(String power) {
this.power = power;
}
public User() {
super();
// TODO 自动生成的构造函数存根
}
public User(String userName, String password, String power) {
super();
this.userName = userName;
this.password = password;
this.power = power;
}
}