初学Java:Java 基础之数据库+GUI实现简单登陆注册窗口
创建登陆注册窗口,实现与数据库之间的链接,将信息存储在数据库
包结构
dao包中的类为数据库操作类,实现与数据库的连接,model类为实体类,util包中为工具类,连接数据库,view包为界面窗体类
源代码
DbUtil类
package jdbctest.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil {
private static String jdbcName = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/jdbc?serveTimezone=GMT%2B8";
private static String userName = "root";
private static String userPwd = "userPwd";
private static Connection con;
public static Connection getCon() throws Exception {
Class.forName(jdbcName);
con = DriverManager.getConnection(url, userName, userPwd);
return con;
}
public static void closeCon() throws Exception {
if (con != null) {
con.close();
}
}
}
LoginFrame类
package jdbctest.view;
import jdbctest.dao.LoginDao;
import jdbctest.model.User;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame extends JFrame{
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
LoginFrame frame = new LoginFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginFrame() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(500, 250, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("企业人事管理系统");
lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setBounds(133, 30, 170, 29);
contentPane.add(lblNewLabel);
JLabel label = new JLabel("用户名:");
label.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
label.setBounds(93, 106, 66, 19);
contentPane.add(label);
textField = new JTextField();
textField.setBounds(171, 103, 132, 26);
contentPane.add(textField);
textField.setColumns(10);
JLabel label_1 = new JLabel("密 码:");
label_1.setBounds(98, 137, 61, 16);
label_1.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
contentPane.add(label_1);
passwordField = new JPasswordField();
passwordField.setBounds(171, 132, 132, 26);
contentPane.add(passwordField);
JButton button = new JButton("登陆");
button.setBounds(93, 192, 99, 29);
contentPane.add(button);
JButton button_1 = new JButton("注册");
button_1.setBounds(253, 192, 99, 29);
contentPane.add(button_1);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String t1 = textField.getText();
String p1 = String.valueOf(passwordField.getPassword());
User user = new User();
user.setUserName(t1);
user.setUserPsw(p1);
try {
new LoginDao(user).ligin();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
button_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
new Register().result();
}
});
}
}
MainFrame类
package jdbctest.view;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class MainFrame extends JFrame{
private JPanel contentPane;
public void mainframe(){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 250, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("欢迎进入主窗口");
label.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
label.setForeground(Color.RED);
label.setBounds(151, 49, 140, 36);
contentPane.add(label);
}
}
Register类
package jdbctest.view;
import jdbctest.dao.UserDao;
import jdbctest.model.User;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Register extends JFrame{
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private JPasswordField passwordField_1;
public void result(){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(500, 250, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("注册新用户");
label.setFont(new Font("Lucida Grande", Font.PLAIN, 17));
label.setBounds(177, 22, 85, 32);
contentPane.add(label);
JLabel label_1 = new JLabel("用户名");
label_1.setBounds(92, 60, 61, 16);
contentPane.add(label_1);
JLabel label_2 = new JLabel("密 码");
label_2.setBounds(92, 99, 34, 16);
contentPane.add(label_2);
JLabel label_3 = new JLabel("确认密码");
label_3.setBounds(92, 139, 61, 16);
contentPane.add(label_3);
textField = new JTextField();
textField.setBounds(165, 55, 130, 26);
contentPane.add(textField);
textField.setColumns(10);
JButton button = new JButton("确定");
button.setBounds(87, 184, 90, 29);
contentPane.add(button);
JButton button_1 = new JButton("退出");
button_1.setBounds(245, 184, 90, 29);
contentPane.add(button_1);
passwordField = new JPasswordField();
passwordField.setBounds(165, 94, 130, 26);
contentPane.add(passwordField);
passwordField_1 = new JPasswordField();
passwordField_1.setBounds(165, 134, 130, 26);
contentPane.add(passwordField_1);
button_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String t1 = textField.getText();
String p1 = String.valueOf(passwordField.getPassword());
String p2 = String.valueOf(passwordField_1.getPassword());
if (t1.length()>0 && p1.length()>0){
if (p1.equals(p2)){
User user = new User();
user.setUserName(t1);
user.setUserPsw(p1);
try {
new UserDao(user).register();
} catch (Exception ex) {
ex.printStackTrace();
}
}else {
JOptionPane.showMessageDialog(null,"密码不一致");
passwordField_1.setText("");
}
}else {
JOptionPane.showMessageDialog(null,"用户名或密码不能为空");
}
}
});
}
}
User类
package jdbctest.model;
public class User {
private String userName;
private String userPsw;
public User(){}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPsw() {
return userPsw;
}
public void setUserPsw(String userPsw) {
this.userPsw = userPsw;
}
}
LoginDao类
package jdbctest.dao;
import jdbctest.model.User;
import jdbctest.util.DbUtil;
import jdbctest.view.MainFrame;
import javax.swing.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class LoginDao {
String name;
String psw;
public LoginDao(User user) {
name = user.getUserName();
psw = user.getUserPsw();
}
public void ligin() throws Exception {
Connection con = DbUtil.getCon();
Statement statement = con.createStatement();
String sql = "select * from jdbctest where userName = '"+name+"' and userPsw = '"+psw+"'";
ResultSet resultSet = statement.executeQuery(sql);
if (resultSet.next()){
new MainFrame().mainframe();
}else {
JOptionPane.showMessageDialog(null,"用户名或密码错误");
}
}
}
UserDao类
package jdbctest.dao;
import jdbctest.model.User;
import jdbctest.util.DbUtil;
import jdbctest.view.LoginFrame;
import jdbctest.view.Register;
import javax.swing.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class UserDao {
String name;
String psw;
public UserDao(User user) {
name = user.getUserName();
psw = user.getUserPsw();
}
public void register() throws Exception {
Connection con = DbUtil.getCon();
Statement statement = con.createStatement();
String sql1 = "select * from jdbctest where userName = '"+name+"' ";
ResultSet result = statement.executeQuery(sql1);
if (result.next()) {
JOptionPane.showMessageDialog(null, "该用户名已存在");
}else {
String sql2 = "insert into jdbctest (userName,userPsw) values ('"+name+"','"+psw+"')";
statement.executeUpdate(sql2);
JOptionPane.showMessageDialog(null,"注册成功");
new LoginFrame().setVisible(true);
}
}
}
运行
以上为本人学习Java时的一些笔记以及自己的理解
如有不足之处请大家指正