在util包中写DbUtil类:定义方法,实现数据库的连接和关闭。 5.在model包中创建用户实体类User 。 包含id、userName、userPwd成员变量;构造方法; getXxx()、setXxx()方法 6.在view包中添加登录窗体类LoginFrame(包含mian方法),MainFrame窗体类。 7.在dao包中: 创建UserDao类,定义添加数据的方法,public int userAdd(Connection con,User user)(在main方法中调用该方法实现向数据库中插入数据); 创建LoginDao类,通过用户名和密码查询用户的方法,public static User login(Connection con,User user) 。 8. 为LoginFrame登录窗体的“登录”按钮和“注册”按钮添加监听器并处理单击事件。登录时,输入正确的用户名和密码,弹出MainFrame窗体;输入错误,弹出提示框。9.为Register注册窗体的“确定”和“取消”按钮添加监听器并处理单击事件。注册成功弹出消息提示框,两次密码不一致,弹出提示框。点击“取消”按钮,textField内容清空,可重新输入。给出java代码

时间: 2024-02-11 07:09:01 浏览: 23
抱歉,我是一名自然语言处理模型,无法提供完整的Java代码。但是,我可以给你提供一些思路和代码片段。 首先,我们需要在util包中创建一个DbUtil类,用于连接和关闭数据库: ```java package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DbUtil { // 数据库连接信息 private static final String URL = "jdbc:mysql://localhost:3306/test"; private static final String USER = "root"; private static final String PASSWORD = "123456"; // 获取数据库连接 public static Connection getConnection() throws SQLException { Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); return conn; } // 关闭数据库连接 public static void closeConnection(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 接着,在model包中创建一个User类,用于表示用户实体: ```java package model; public class User { private int id; private String userName; private String userPwd; public User() {} public User(int id, String userName, String userPwd) { this.id = id; this.userName = userName; this.userPwd = userPwd; } // getter和setter方法 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 getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } } ``` 然后,在dao包中创建一个UserDao类,用于实现用户数据的添加: ```java package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import model.User; import util.DbUtil; public class UserDao { // 添加用户数据 public static int userAdd(Connection con, User user) throws SQLException { String sql = "insert into user (id,userName,userPwd) values (?,?,?)"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, user.getId()); pstmt.setString(2, user.getUserName()); pstmt.setString(3, user.getUserPwd()); int result = pstmt.executeUpdate(); pstmt.close(); return result; } } ``` 接着,在dao包中创建一个LoginDao类,用于实现用户的登录: ```java package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import model.User; import util.DbUtil; public class LoginDao { // 登录验证 public static User login(Connection con, User user) throws SQLException { User resultUser = null; String sql = "select * from user where userName=? and userPwd=?"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, user.getUserName()); pstmt.setString(2, user.getUserPwd()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { resultUser = new User(); resultUser.setId(rs.getInt("id")); resultUser.setUserName(rs.getString("userName")); resultUser.setUserPwd(rs.getString("userPwd")); } rs.close(); pstmt.close(); return resultUser; } } ``` 接着,在view包中创建一个LoginFrame类,用于实现登录窗体: ```java package view; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.SQLException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import dao.LoginDao; import model.User; import util.DbUtil; public class LoginFrame extends JFrame { private JPanel contentPane; private JTextField userNameField; private JPasswordField passwordField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { LoginFrame frame = new LoginFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public LoginFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("用户名:"); lblNewLabel.setBounds(81, 78, 54, 15); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("密码:"); lblNewLabel_1.setBounds(81, 123, 54, 15); contentPane.add(lblNewLabel_1); userNameField = new JTextField(); userNameField.setBounds(145, 75, 156, 21); contentPane.add(userNameField); userNameField.setColumns(10); passwordField = new JPasswordField(); passwordField.setBounds(145, 120, 156, 21); contentPane.add(passwordField); JButton loginButton = new JButton("登录"); loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String userName = userNameField.getText(); String userPwd = new String(passwordField.getPassword()); User user = new User(); user.setUserName(userName); user.setUserPwd(userPwd); Connection conn = null; try { conn = DbUtil.getConnection(); User resultUser = LoginDao.login(conn, user); if (resultUser != null) { MainFrame frame = new MainFrame(); frame.setVisible(true); dispose(); } else { JOptionPane.showMessageDialog(null, "用户名或密码错误"); } } catch (SQLException e1) { e1.printStackTrace(); } finally { DbUtil.closeConnection(conn); } } }); loginButton.setBounds(81, 185, 93, 23); contentPane.add(loginButton); JButton registerButton = new JButton("注册"); registerButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { RegisterFrame frame = new RegisterFrame(); frame.setVisible(true); dispose(); } }); registerButton.setBounds(208, 185, 93, 23); contentPane.add(registerButton); } } ``` 接着,在view包中创建一个MainFrame类,用于实现主窗体: ```java package view; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class MainFrame extends JFrame { private JPanel contentPane; /** * Create the frame. */ public MainFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); } } ``` 最后,在view包中创建一个RegisterFrame类,用于实现注册窗体: ```java package view; import java.awt.EventQueue; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import dao.UserDao; import model.User; import util.DbUtil; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.SQLException; import java.awt.event.ActionEvent; import javax.swing.JPasswordField; public class RegisterFrame extends JFrame { private JPanel contentPane; private JTextField userNameField; private JPasswordField passwordField; private JPasswordField confirmPasswordField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { RegisterFrame frame = new RegisterFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public RegisterFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("用户名:"); lblNewLabel.setBounds(81, 56, 54, 15); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("密码:"); lblNewLabel_1.setBounds(81, 101, 54, 15); contentPane.add(lblNewLabel_1); JLabel lblNewLabel_2 = new JLabel("确认密码:"); lblNewLabel_2.setBounds(81, 146, 68, 15); contentPane.add(lblNewLabel_2); userNameField = new JTextField(); userNameField.setBounds(145, 53, 156, 21); contentPane.add(userNameField); userNameField.setColumns(10); passwordField = new JPasswordField(); passwordField.setBounds(145, 98, 156, 21); contentPane.add(passwordField); confirmPasswordField = new JPasswordField(); confirmPasswordField.setBounds(145, 143, 156, 21); contentPane.add(confirmPasswordField); JButton okButton = new JButton("确定"); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String userName = userNameField.getText(); String userPwd = new String(passwordField.getPassword()); String confirmPassword = new String(confirmPasswordField.getPassword()); if (userPwd.equals(confirmPassword)) { User user = new User(); user.setId(1); user.setUserName(userName); user.setUserPwd(userPwd); Connection conn = null; try { conn = DbUtil.getConnection(); int result = UserDao.userAdd(conn, user); if (result > 0) { JOptionPane.showMessageDialog(null, "注册成功"); dispose(); LoginFrame frame = new LoginFrame(); frame.setVisible(true); } else { JOptionPane.showMessageDialog(null, "注册失败"); } } catch (SQLException e1) { e1.printStackTrace(); } finally { DbUtil.closeConnection(conn); } } else { JOptionPane.showMessageDialog(null, "两次密码不一致"); } } }); okButton.setBounds(81, 191, 93, 23); contentPane.add(okButton); JButton cancelButton = new JButton("取消"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { userNameField.setText(""); passwordField.setText(""); confirmPasswordField.setText(""); } }); cancelButton.setBounds(208, 191, 93, 23); contentPane.add(cancelButton); } } ``` 以上是一个简单的基于Java Swing和MySQL的用户登录和注册系统的实现。

相关推荐

最新推荐

recommend-type

使用抽象类继承实现:“剪刀石头布的游戏”

使用抽象类继承实现:“剪刀石头布的游戏” 问题 思路分析(有助于你们去分析如何写这类问题) 运行结果演示 演示1: 演示2: package 剪子布暴捶; /* 姓名:马志勇 许昌学院 互注 互助 互祝 有问题联系QQ:...
recommend-type

微信小程序提取公用函数到util.js及使用方法示例

主要介绍了微信小程序提取公用函数到util.js及使用方法,结合实例形式分析了util.js公用函数相关调用操作技巧,需要的朋友可以参考下
recommend-type

Android 出现:java.lang.NoClassDefFoundError...错误解决办法

主要介绍了Android 出现:Android出现:java.lang.NoClassDefFoundError: android/os/PersistableBundle错误解决办法的相关资料,需要的朋友可以参考下
recommend-type

mysql数据库连接池配置教程

第一步:写javabean 代码如下: package withouttears.jdbc.db; import java.util.HashMap; import java.sql.*;... //数据库资源的连接工厂是javax.sql.DataSource对象, //它可以创建java.sql.Connec
recommend-type

微信小程序-番茄时钟源码

微信小程序番茄时钟的源码,支持进一步的修改。番茄钟,指的是把工作任务分解成半小时左右,集中精力工作25分钟后休息5分钟,如此视作种一个“番茄”,而“番茄工作法”的流程能使下一个30分钟更有动力。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。