2、登录的实现和注册实现

本文档详细介绍了使用Java Swing构建登录和注册界面的实现过程,包括登录验证、数据库查询以及注册时的必填项和密码一致性验证。通过监听事件处理按钮操作,利用PreparedStatement执行SQL语句,实现了与users表的交互。注册功能中,对用户输入进行了完整性检查,并将新用户信息插入数据库。
摘要由CSDN通过智能技术生成

上篇中完成了登陆界面的实现

这篇中来完成登录的实现和注册实现

1、登录的实现

要完成登录账号密码是必不可少的
所以再users表中的id和passwd就是用来实现登陆的
首先我们需要在文本框中输入值然后在数据库中进行匹配
这些写在登录按钮的监听事件中
代码如下:
监听事件

loging.addActionListener(e->{
	try {
		String tilitl=new mysql().Selectuser(useridtxt.getText(), userpwdtxt.getText());
		if(tilitl.equals("admin"))
		{
			new admin.adminIndexJFram().Start();
			log.setVisible(false);
		}
		if(tilitl.equals("user"))
		{
			System.out.println("user ");
		}
		if(tilitl.equals("空"))
		{
			JOptionPane.showMessageDialog(null, "账号或密码错误!");
		}
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	});

数据库查询

public String Selectuser(String adminid,String adminpwd) throws SQLException
	{
		Connection counect = new util.mysqlUtil().counect();
		PreparedStatement Statement=null;
		try {
			Statement=counect.prepareStatement("select tilitl from users where userid=? and userpasswd=?");
			Statement.setString(1, adminid);
			Statement.setString(2, adminpwd);			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultSet rs=Statement.executeQuery();
		if(rs.next())
		{
			return rs.getString("tilitl");
		}
		return "空";
	}

这里的查询返回值为String是为了判断账号密码对应的是普通用户还是管理员
所用的区分正是前面提到的 tilitl 字段返回对应的结果就启动相应的界面

2、注册的实现

首先在注册按钮的监听时间中启动注册页面

	reg.addActionListener(e->{
	new regJFrame().Start();
	log.setVisible(false);		
	});

注册页面部分代码如下:

public void Start()
{
	JFrame reg=new JFrame();
	JLabel username=new JLabel("昵称:");
	userid.setHorizontalAlignment(SwingConstants.CENTER);
	//设置文字居中
	
	JLabel userpwd=new JLabel("密码:");
	userpwd.setHorizontalAlignment(SwingConstants.CENTER);
	
	JLabel reuserpwd=new JLabel("确认密码:");
	reuserpwd.setHorizontalAlignment(SwingConstants.CENTER);
	
	JTextField useridtxt=new JTextField();
	JPasswordField userpwdtxt=new JPasswordField();
	JPasswordField reuserpwdtxt=new JPasswordField();
	JButton chck=new JButton("确定");
	 .
	 .
	 .
	 JPanel j=new JPanel(new GridLayout(4,2));

这里的布局使用的网格布局器
效果如下:
在这里插入图片描述

然后注册根据前面的users表的相应字段来设置参数
id自增不用写
passwd – > userpwdtxt.getTxt()
name-- >username控件
注册首先实现必填验证
然后是验证两次密码是否相同
我就用.equals(value)来实现
然后就是连接数据库写数据

确认按钮监听时间代码如下:

chck.addActionListener(e->{
		if(useridtxt.getText().equals("")||userpwdtxt.getText().equals("")||reuserpwdtxt.getText().equals(""))
			{
			JOptionPane.showMessageDialog(null,"请完善信息!");
			}
		else {
			if(userpwdtxt.getText().equals(reuserpwdtxt.getText()))
			{
				try {
					new mysql().insertuser(userpwdtxt.getText(), useridtxt.getText());
					JOptionPane.showMessageDialog(null,"你的账号为:"+new mysql().Selectuserid(useridtxt.getText()));
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			else {
				JOptionPane.showMessageDialog(null,"两次密码输入不一致!");
			}
		}
		});

数据代码如下:

public boolean insertuser(String id,String pwd) throws SQLException
	{
		Connection counect = new util.mysqlUtil().counect();
		boolean a=false;
		PreparedStatement Statement=null;
		try {
			Statement=counect.prepareStatement("insert into users(userpasswd,username,tilitl) values(?,?,'user');");	
			Statement.setString(1,id );
			Statement.setString(2,pwd);
			a=Statement.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}	

因为注册的是普通用户所以tilitl为user
效果如下:

必填项验证

在这里插入图片描述

重复密码验证:

在这里插入图片描述

正确注册获取账号:

在这里插入图片描述
数据库结果:

在这里插入图片描述

至此登录和注册均已实现

感谢阅读!!

管理员的内容添加会在下一篇实现

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值