通过用户注册可以有效采集用户信息,并将合法的用户信息保存到指定的数据表中。本项目中用户主要指学生用户和教师用户。用户只有在注册成功并登录后才可以进入主界面,使用相应模块功能。
本项目分别编写了学生用户和教师用户注册,新用户注册需要填写的信息有个人信息和账号信息,由于页面部分代码量较大,这里以学生注册为例进行讲解。
1、编写注册页面
在view包中新建RegisterOptFrm类,在RegisterOptFrm类中按身份选择分别编写教师和学生注册。身份选择的部分构建代码如下:
JLabel lblNewLabel = new JLabel("选择注册身份");
lblNewLabel.setFont(new Font("黑体", Font.BOLD, 25));
JButton btnNewButton_1 = new JButton("教师");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new UserRegisterFrm().setVisible(true);
dispose();
}
});
btnNewButton_1.setFont(new Font("黑体", Font.PLAIN, 12));
JButton btnNewButton_2 = new JButton("学生");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new StuRegisterFrm(0).setVisible(true);
dispose();
}
});
btnNewButton_2.setFont(new Font("黑体", Font.PLAIN, 12));
选择学生身份注册后,在编写StuRegisterFrm类用于学生注册信息。需要输入学生基本信息,如学号、姓名、年龄、班级、性别等个人信息,账号、密码等账户信息。
选择教师身份注册后,在编写UserRegisterFrm类用于教师注册信息。需要输入账号、密码等账户信息。
2、编写注册监听器
填写学生注册信息之后,单击注册按钮完成注册。在单击注册按钮后,注册按钮会对所填注册信息的正确性、完整性进行判断。学生用户注册按钮监听器实现代码如下:
private void userRegisterActionPerformed(ActionEvent evt) {
String stuid=this.stuid.getText();//得到学号
String stuname=this.stuname.getText();//得到姓名
String stuage=this.stuage.getText();//得到年龄
String stuclass=this.stuclass.getText();//得到班级
String stusex=this.stusex.getText();//得到性别
String uname=this.stuRegisterText.getText();//得到用户名
String pass1=new String(this.stuRegisterPas1.getPassword());//得到第一个密码
String pass2=new String(this.stuRegisterPas2.getPassword());//得到第二个密码
if(StringUtil.isEmpty(stuid)) {//用户名为空
JOptionPane.showMessageDialog(null, "学号不能为空!");
return;
}
if(StringUtil.isEmpty(stuname)) {//姓名为空
JOptionPane.showMessageDialog(null, "姓名不能为空!");
return;
}
if(StringUtil.isEmpty(stuage)) {//年龄为空
JOptionPane.showMessageDialog(null, "年龄不能为空!");
return;
}
if(StringUtil.isEmpty(stuclass)) {//班级为空
JOptionPane.showMessageDialog(null, "班级不能为空!");
return;
}
if(StringUtil.isEmpty(stusex)) {//性别为空
JOptionPane.showMessageDialog(null, "性别不能为空!");
return;
}
if(StringUtil.isEmpty(uname)) {//用户名为空
JOptionPane.showMessageDialog(null, "用户名不能为空!");
return;
}
if(StringUtil.isEmpty(pass1)||StringUtil.isEmpty(pass2)) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
return;
}
if(pass1.length()<6) {
JOptionPane.showMessageDialog(null, "密码不能少于6位!");
return;
}
if(!pass1.equals(pass2)) {
JOptionPane.showMessageDialog(null, "输入密码不一致!");
return;
}
Student stu=new Student(stuid,stuname,Integer.parseInt(stuage),stuclass,stusex,uname,pass1);//创建学生对象
Connection con=null;
try {
con=stuutil.getCon();
if(StudentDao.inquiry(con,stu)) {
JOptionPane.showMessageDialog(null, "该用户名已存在!");
return;
}
if(StudentDao.inquiryId(con,stu)) {
JOptionPane.showMessageDialog(null, "该学生已有账号!");
return;
}
int registerUser=StudentDao.register(con, stu);
if(registerUser==1) {
JOptionPane.showMessageDialog(null, "注册成功!");
}
else {
JOptionPane.showMessageDialog(null, "注册失败!");
return;
}
stuutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
教师用户填写注册信息后,单击注册按钮,完成注册。教师注册按钮监听器代码如下:
String uname=this.stuRegisterText.getText();//得到用户名
String pass1=new String(this.stuRegisterPas1.getPassword());//得到第一个密码
String pass2=new String(this.stuRegisterPas2.getPassword());//得到第二个密码
User user = null;
if(StringUtil.isEmpty(uname)) {//用户名为空
JOptionPane.showMessageDialog(null, "用户名不能为空!");
return;
}
if(StringUtil.isEmpty(pass1)||StringUtil.isEmpty(pass2)) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
return;
}
if(pass1.length()<6) {
JOptionPane.showMessageDialog(null, "密码不能少于6位!");
return;
}
if(!pass1.equals(pass2)) {
JOptionPane.showMessageDialog(null, "输入密码不一致!");
return;
}
user=new User(uname,pass1,"教师");
Connection con=null;
try {
con=stuutil.getCon();
if(UserDao.inquiry(con, user)) {
JOptionPane.showMessageDialog(null, "该用户名已存在!");
return;
}
int registerUser=UserDao.register(con, user);
if(registerUser==1) {
JOptionPane.showMessageDialog(null, "注册成功!");
}
else {
JOptionPane.showMessageDialog(null, "注册失败!");
return;
}
stuutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
3、编写dao层
在dao包中编写StudentDao类,在StudentDao类中编写register()方法,用于完成学生注册操作。register()方法代码如下:
public static int register(Connection con,Student stu)throws Exception{
String sql="insert into studentuser values(?,?,?,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, stu.getStuid());
pstmt.setString(2, stu.getName());
pstmt.setLong(3, stu.getAge());
pstmt.setString(4, stu.getStuclass());
pstmt.setString(5, stu.getSex());
pstmt.setString(6, stu.getStuname());
pstmt.setString(7, stu.getPassword());
return pstmt.executeUpdate();
}
在dao包中编写UserDao类,在UserDao类中编写register()方法,用于完成教师注册操作。register()方法代码如下:
public static int register(Connection con,User user) throws Exception{
String sql="";
if(user.getIden().equals("教师")) {
sql="insert into teacheruser (uname,upassword)values(?,?)";
}
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setNString(1, user.getName());
pstmt.setNString(2, user.getPassWord());
return pstmt.executeUpdate();
}
用户注册成功之后,便可以在校园疫情管理系统中登录页面进行登录操作。用户登录时,需要输入用户名和密码,并选择登录权限。下面分步骤讲解用户登录的实现。
1、编写登录页面
在view包中编写LogOnFrm类,在LogOnFrm类中编写用户登录时需要填写的用户名、密码和身份权限等文本框和下拉框组件。部分代码如下:
JLabel lblNewLabel = new JLabel("校园疫情防控管理系统");
lblNewLabel.setFont(new Font("黑体", Font.BOLD, 32));
JLabel lblNewLabel_2 = new JLabel("密 码:");
lblNewLabel_2.setFont(new Font("黑体", Font.PLAIN, 20));
passwordText = new JPasswordField();
JLabel lblNewLabel_1 = new JLabel("用户名:");
lblNewLabel_1.setFont(new Font("黑体", Font.PLAIN, 20));
userNameText = new JTextField();
userNameText.setColumns(10);
JButton btnNewButton = new JButton("登录");
btnNewButton.setFont(new Font("黑体", Font.PLAIN, 12));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loginActionPerformed(e);
}
});
JLabel lblNewLabel_3 = new JLabel("身 份:");
lblNewLabel_3.setFont(new Font("黑体", Font.PLAIN, 20));
identity = new JComboBox();
identity.setModel(new DefaultComboBoxModel(new String[] {"教师", "学生"}));
2、编写登录按钮的监听器
用户填写完登录信息后,单击登录按钮进行登录时,需要判断用户信息的正确性和完整性,需要为登录按钮添加事件监听器。登录按钮监听器代码如下:
private void loginActionPerformed(ActionEvent evt) {
String userName=this.userNameText.getText();//取得账号
String password=new String(this.passwordText.getPassword());//取得密码
String iden=(String) identity.getSelectedItem();
if(StringUtil.isEmpty(userName)) {//用户名不为空
JOptionPane.showMessageDialog(null, "用户名不能为空!");
return;
}
if(StringUtil.isEmpty(password)) {//密码不为空
JOptionPane.showMessageDialog(null, "密码不能为空!");
return;
}
User user=new User(userName,password,iden);//创建用户对象
Connection con=null;
try {
con=stuutil.getCon();
User currentUser=userDao.login(con, user);
if(currentUser!=null) {
JOptionPane.showMessageDialog(null, "登录成功!");
dispose();//销毁窗口
if(user.getIden().equals("教师")){
new TeacherMainFrm().setVisible(true);//创建教师窗口
dispose();
}else if(user.getIden().equals("学生")) {
new StuMainFrm(currentUser).setVisible(true);//创建学生窗口
dispose();
}
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
stuutil.closeCon(con);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
3、编写dao层
在UserDao类中,添加login()方法,用于从数据库查询用户。login()方法的实现代码如下:
public User login(Connection con,User user)throws Exception{
User resultUser=null;
String sql="select *from "+StringUtil.isIden(user.getIden())+" where uname=? and upassword=? ";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,user.getName());//给第一个?赋值
pstmt.setString(2, user.getPassWord());//给第二个?赋值
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
resultUser=new User();
resultUser.setId(rs.getString("uid"));
resultUser.setName(rs.getString("uname"));
resultUser.setName(rs.getString("upassword"));
}
return resultUser;
}