功能介绍
注册与登录是项目中的基本功能,注册个人信息,然后验证登录后才能使用软件。
功能实现原理
注册原理:
通过在数据库的 register表 进行添加数据,来实现添加了新的用户。
登录原理:
通过输入的数据与数据库中 register表 进行比较,若匹配成功,则是存在该用户,若匹配失败,则是该用户不存在或输入的学号或密码不匹配。
注册功能
//注册方法
public int register(UserData user) {
//通过帮助类获取数据库链接
Connection con=DBUtils.getConnection();
int count=0;
//获取预处理对象操作数据库
try {
PreparedStatement pst =con.prepareStatement("insert into register(Sno,id,password,phone) values(?,?,?,?)");
//解析占位符
pst.setString(1, user.getSno());
pst.setString(2, user.getId());
pst.setString(3, user.getPassword());
pst.setString(4, user.getPhone());
//执行sql
count=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
//注册时需要主要的方法:
/*
* 注册需要判断是否是真实的学生学号
* 还有学号是否被注册过
*/
//注册时检验是否有存在该学号(确认是学生才能注册)
public UserData isExistS(String Sno) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
UserData user=null;
//获取预处理对象
try {
//通过帮助类获取数据库链接对象
con=DBUtils.getConnection();
pst =con.prepareStatement("select * from Student where Sno=?");
//解析占位符
pst.setString(1, Sno);
//执行sql
rs =pst.executeQuery();
//遍历结果集
while(rs.next()){
user =new UserData();
user.setSno(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
//注册时检验是否使用过该学号
public UserData isExistR(String Sno) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
UserData user=null;
//获取预处理对象
try {
//通过帮助类获取数据库链接对象
con=DBUtils.getConnection();
pst =con.prepareStatement("select * from register where Sno=?");
//解析占位符
pst.setString(1, Sno);
//执行sql
rs =pst.executeQuery();
//遍历结果集
while(rs.next()){
user =new UserData();
user.setSno(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
/*
* 用于检验号码是否用过
* 一个号码只能对应一个账号
* 便于后期忘记密码可找回
*/
public UserData isPhone(String phone) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
UserData user=null;
//获取预处理对象
try {
//通过帮助类获取数据库链接对象
con=DBUtils.getConnection();
pst =con.prepareStatement("select * from register where phone=?");
//解析占位符
pst.setString(1, phone);
//执行sql
rs =pst.executeQuery();
//遍历结果集
while(rs.next()){
user =new UserData();
user.setPhone(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
注册界面:
登录功能
/*
* 验证登陆时是否存在用户(检验学号加密码)
* 由于是对学生开放使用
* 所以对学生要求学号
*/
public UserData login(String Sno, String password) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
UserData user=null;
//获取预处理对象
try {
//通过帮助类获取数据库链接对象
con=DBUtils.getConnection();
pst =con.prepareStatement("select * from register where Sno=? and password=?");
//解析占位符
pst.setString(1, Sno);
pst.setString(2, password);
//执行sql
rs =pst.executeQuery();
//遍历结果集
while(rs.next()){
user =new UserData();
user.setSno(rs.getString(1));
user.setPassword(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
登录界面:
在此基础上还可实现 忘记密码和找回密码,在之后的博客会有实现这两个功能
下一篇是 (3)课室查询功能的实现