📄 login.java
字号:
package javaBeanClass.Login;
import javaBeanClass.DatabaseConn;
import javaBeanClass.MyException;
import java.util.*;
import java.sql.*;
public class Login
{
private DatabaseConn db;
private int ID;
private String Name;
private String Passwd;
private String newPasswd;
private String Power;
private String yzm="";//验证码
private boolean update=false;
private int ClassID;
private String sql;
public void setID(int ID){this.ID=ID;}
public int getID(){return this.ID;}
public void setName(String Name)
{this.Name=Name;}
public String getName(){return this.Name;}
public void setPasswd(String Passwd)
{this.Passwd=Passwd;}
public String getPasswd(){return this.Passwd;}
public void setNewPasswd(String newPasswd)
{this.newPasswd=newPasswd;}
public String getNewPasswd(){return this.newPasswd;}
public void setPower(String Power){this.Power=Power;}
public String getPower(){return this.Power;}
public void setYzm(String yzm){this.yzm=yzm;}
public String getYzm(){return this.yzm;}
public int getClassID(){return this.ClassID;}
public Login(){db=new DatabaseConn();}
public void checkLogin(String User)throws SQLException,MyException
{
try{checkHeFa();}catch(MyException me){throw new MyException(me.getMessage());}
if(User.equals("Teacher")){
sql="select ID,Passwd,Power from Teacher where Name=? And Del='0'";
}
else{
sql="select ID,Passwd,ClassID from UserInfo where SN=? and Del='0'";
}
boolean rowCount=false;
String Passwd="";
Connection con;
PreparedStatement ps=null;
ResultSet rs=null;
try{con=db.connectToDB();}
catch(Exception ex){throw new SQLException("Connect database failed!");}
try
{
ps=con.prepareStatement(sql);
ps.setString(1,this.Name);
rs=ps.executeQuery();
rowCount=rs.next();
if(rowCount)
{
this.ID=rs.getInt(1);
Passwd=rs.getString(2);
if(User.equals("Teacher")){
this.Power=rs.getString(3);
}
else{
this.ClassID=rs.getInt(3);
}
}
}
catch(Exception ex)
{
System.out.println("Check Login failed:"+ex.getMessage());
throw new SQLException("Check Login failed!");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("CheckLogin Close Login Error:"+sqlex.getMessage());}
}
if(rowCount)
{
if(!this.Passwd.equals(Passwd))
{throw new MyException("密码错误!");}
}
else
{throw new MyException("用户不存在!");}
}
public void updatePasswd(String User)throws SQLException,MyException
{
try
{
this.update=true;
checkLogin(User);
}
catch(MyException me)
{throw new MyException(me.getMessage());}
catch(Exception e)
{throw new SQLException(e.getMessage());}
if(User.equals("Teacher")){
sql="update Teacher set Passwd=? where ID=?";
}
else{
sql="update UserInfo set Passwd=? where ID=?";
}
Connection con;
PreparedStatement ps=null;
try{con=db.connectToDB();}
catch(Exception ex){throw new SQLException("Connect database failed!");}
try
{
ps=con.prepareStatement(sql);
ps.setString(1,this.newPasswd);
ps.setInt(2,this.ID);
int rowCount=ps.executeUpdate();
if(rowCount==0)
{
throw new SQLException("Update Login failed!");
}
}
catch(SQLException sqlex)
{
System.err.println("Update Login Error:"+sqlex.getMessage());
throw new SQLException("Update Login Error");
}
catch(Exception e)
{
System.out.println("Other Of Update Login Error:"+e.getMessage());
throw new SQLException("Other Of Update Login Error");
}
finally
{
try{con.close();ps.close();}
catch(SQLException sqlex){System.out.println("Update Close Login Error:"+sqlex.getMessage());}
}
throw new MyException("已成功更改密码,请到登录页用新密码登录!");
}
public void checkHeFa()throws MyException//检测用户名及密码的合法性
{
if(this.update)
{
if(this.Name==null) throw new MyException("请正确填写密码信息!");
}
else
{
if(this.Name==null) throw new MyException("请正确填写登录信息!");
}
//检测名称和密码是否有非法字符
if(false)
{throw new MyException("用户名和密码中含有非法字符!");}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -