1、创建数据库和表
1)创建学生数据库
CREATE DATABASE student CHARSET='utf8mb4';
2) 创建用户表
3)插入记录
2、创建用户实体类
在s09.bean里创建User类
package bean;
import java.util.Date;
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
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 getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
'}';
}
}
3、添加数据库驱动包
- 在项目根目录里创建libs目录,添加数据库驱动包
4、创建数据库连接管理类
在s09.dbutils包中创建ConnectionManager类
package dbutils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/student?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "111111";
private ConnectionManager() {
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("提示:数据库连接成功~");
} catch (ClassNotFoundException e) {
System.err.println("异常:数据库驱动程序未找到!");
} catch (SQLException e) {
System.err.println("异常:数据库连接失败!");
}
return conn;
}
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
if (!conn.isClosed()){
conn.close();
System.out.println("提示:数据库连接关闭~");
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
}
public static void main(String[] args) {
Connection conn = getConnection();
closeConnection(conn);
}
}
5、创建用户数据访问接口
package dao;
import s09.bean.User;
import java.util.List;
public interface UserDao {
int insert(User user);
int delete(int id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
}
6、创建用户数据访问接口实现类
- 在s09.dao.impl包中创建UserDaoImpl类
package dao.impl;
import dao.UserDao;
import dbutils.ConnectionManager;
import s09.bean.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
@Override
public int insert(User user) {
return 0;
}
@Override
public int delete(int id) {
return 0;
}
@Override
public int update(User user) {
return 0;
}
@Override
public User findById(int id) {
return null;
}
@Override
public List<User> findAll() {
List<User> users = new ArrayList<>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("password"));
user.setPassword(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}finally {
ConnectionManager.closeConnection(conn);
}
return users;
}
@Override
public User login(String username, String password) {
User user = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_user where username = ? and password = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
}
7、创建用户服务类
- 在s09.service包中创建UserService类
package service;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import s09.bean.User;
import java.util.List;
public class UserService {
private UserDao userDao;
public UserService() {
userDao = new UserDaoImpl();
}
public int addUser(User user) {
return userDao.insert(user);
}
public int deleteUser(int id) {
return userDao.delete(id);
}
public int updateUser(User user) {
return userDao.update(user);
}
public User findUserById(int id) {
return userDao.findById(id);
}
public List<User> findAllUsers() {
return userDao.findAll();
}
public User login(String username, String password) {
return userDao.login(username, password);
}
}
8、创建用户登录界面
package gui;
import service.UserService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class LoginFrame extends JFrame {
private String username;
private String password;
private JLabel lblUsername;
private JLabel lblPassword;
private JTextField txtUsername;
private JPasswordField txtPassword;
private JButton btnOK;
private JButton btnCancel;
private JPanel panel, panel1, panel2, panel3;
public LoginFrame(String title) {
super(title);
initGUI();
eventsHandling();
}
private void eventsHandling() {
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
username = txtUsername.getText().trim();
password = new String(txtPassword.getPassword());
UserService userService = new UserService();
s09.bean.User user = userService.login(username, password);
if (user != null) {
JOptionPane.showMessageDialog(null, "恭喜,[" + username + "]登录成功~");
dispose();
} else {
JOptionPane.showMessageDialog(null, "遗憾,用户名或密码错误~");
txtUsername.setText("");
txtPassword.setText("");
txtUsername.requestFocus();
}
}
});
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
private void initGUI() {
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField(15);
txtPassword = new JPasswordField(15);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel.setLayout(new GridLayout(3, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
txtPassword.setEchoChar('*');
setSize(250, 200);
setLocationRelativeTo(null);
setResizable(false);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new LoginFrame("用户登录");
}
}
查看结果
|