2024年2月3日发(作者:)
java 用户登录界面
在所有的管理程序中,都不可缺少的即用户登录界面,用于指管理人员使用特定的管理权限。
在前面章节中,我们已经介绍过通过Java中的Swing制作登录界面,但是登录界面中的用户名与密码等信息,并没有与数据库进行连接。本实例,我们主要介绍如何通过登录界面,判断用户名与密码是否正确。
操作步骤:
(1)导入类包和创建主类,然后定义所需要的变量。然后,创建init( )用来显示用户登录界面。由于前已经介绍比较详细,在此不再详细介绍。
import .*;
import .*;
import .*;
import .*;
public class Register implements ActionListener {
TextField t1; JPasswordField t2; JButton b1, b2; JFrame f; JPanel p1; JLabel l1, l2;
void init() {
f = new JFrame("登录窗口");
p1 = new JPanel();
l1 = new JLabel("用户名", );
l2 = new JLabel("密码", );
t1 = new TextField(11);
t2 = new JPasswordField(11);
b1 = new JButton("登录");
b2 = new JButton("取消");
out(new FlowLayout());// 流式布局管理
aultCloseOperation(_ON_CLOSE);
e(300, 150);
out(new GridLayout(3, 2));
(l1); (t1); (l2); (t2); (b1); (b2); (p1);ible(true);
ionListener(this);
ionListener(new ActionListener() {// 为取消按钮添加时间处理
public void actionPerformed(ActionEvent ee) {
(0);
}
});
}
(2)创建监听方法,并通过“String u1 = t();”和“String p1 = new
String(sword());”分别获取界面中用户名和密码信息。
然后,通过if else语句来判断用户名和密码是否为空,若不为空则实例化lianjie( u1,p1)方法,并将用户名和密码信息传递过去。
public void actionPerformed(ActionEvent e) {
String u1 = t();
String p1 = new String(sword());
if (("") && ("")) {
ssageDialog(null, "用户名和密码不能为空!", "警告",
G_MESSAGE);
} else {
try {
}
new lianjie(u1, p1);
tackTrace();
} catch (Exception e1) {
}
}
public static void main(String[] args) {
}
new Register().init();
}
(3)创建lianjie类,并构造该类的方法。然后,连接数据库,并通过“select *
from user”语句查询数据表中,用户名和密码信息,并返回数据集。
class lianjie {
String name;
String Password;
public lianjie(String u1, String p1) throws Exception {
try {
e("").newInstance();
String url = "jdbc:mysql://localhost:3306/studentmanage";
String user = "mysql";
String password = "123";
Connection conn = nection(url, user, password);
// 创建用于将 SQL 语句发送到数据库的 SQLServerStatement 对象。
Statement st = Statement();
String sql = "select * from user";
ResultSet rs = eQuery("select * from user");
(4)移动数据表中的指针,并通过do while循环语句,来遍历结果集中的数据,通过if语句判断用户名和密码是否与登录界面中输入的用户名和密码相同。
();
}
do {
name = ing("use");
Password = ing("pw");
if ((name) || (Password)) {
ssageDialog(null, "恭喜你,登录成功!", "提示",
G_MESSAGE);
} else {
ssageDialog(null, "登陆失败,用户名或密码错误!", "警告",
}
G_MESSAGE);
} while (());
();
} catch (SQLException ee) {
}
}
(5)执行代码,弹出【登录窗口】窗口,并输入用户名和密码内容,单击【登录】按钮。若输入错误,则弹出【警告】提示框,并显示“登录失败,用户名或密码错误!”。若输入正确定,则提示“恭喜你,登录成功!”,如图13-2所示。
输入用户名和密码 用户名和密码错误 用户名和密码正确
图13-2 用户登录
本文发布于:2024-02-03 11:58:24,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170693270450299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |