📄 admin.java
字号:
/*
* 创建日期 2005-3-7
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package store;
/**
* @author wwx
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import java.sql.ResultSet;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import store.DBConnect;
public class Admin {
private String AdminUser;
private String AdminPass;
public Admin()
{
}
public void SetAdminuser(String s)
{
AdminUser = s;
}
public void SetAdminpass(String s)
{
AdminPass = s;
}
public String GetAdminuser()
{
return AdminUser;
}
public String GetAdminpass()
{
return AdminPass;
}
public void AddAdmin(HttpServletRequest httpservletrequest) throws Exception
{
String s = null;
boolean flag = false;
String s1 = ParamUtils.getString(httpservletrequest, "adminuser", "");
String s2 = ParamUtils.getString(httpservletrequest, "password", "");
if(s1.equals("") || s1 == null)
{
s = s + "You have not input the administrator's name!<br>";
flag = true;
}
if(s2.equals("") || s2 == null)
{
s = s + "You have not input the password!<br>";
flag = true;
}
if(!flag)
{
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("insert into My_BookAdminuser (adminuser,adminpass) values (?,?)");
dbconnect.setBytes(1,s1.getBytes("GBK"));
dbconnect.setBytes(2, s2.getBytes("GBK"));
dbconnect.executeUpdate();
dbconnect.close();
}
else
{
throw new Exception(s);
}
}
public void ChangePassword(HttpServletRequest httpservletrequest) throws Exception
{
String s = null;
boolean flag = false;
String s1 = ParamUtils.getString(httpservletrequest, "adminuser", "");
String s2 = ParamUtils.getString(httpservletrequest, "password", "");
if(s1.equals("") || s1 == null)
{
s = s + "You have not input the administrator's name!<br>";
flag = true;
}
if(s2.equals("") || s2 == null)
{
s = s + "You have not input the password!<br>";
flag = true;
}
if(!flag)
{
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("update My_BookAdminuser set adminpass=? where adminuser=?");
dbconnect.setBytes(1, s2.getBytes("GBK"));
dbconnect.setBytes(2, s1.getBytes("GBK"));
dbconnect.executeUpdate();
dbconnect.close();
}
else
{
throw new Exception(s);
}
}
public Vector LoadAdmin() throws Exception
{
Vector vector = new Vector();
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("select * from My_BookAdminuser");
Admin admin;
for(ResultSet resultset = dbconnect.executeQuery(); resultset.next(); vector.add(admin))
{
admin = new Admin();
admin.SetAdminuser(resultset.getString("adminuser"));
admin.SetAdminpass(resultset.getString("adminpass"));
}
dbconnect.close();
return vector;
}
public static void LoginUser(HttpServletRequest httpservletrequest)throws Exception
{
boolean flag = false;
String s = "";
String s1 = ParamUtils.getString(httpservletrequest, "adminuser", "");
String s2 = ParamUtils.getString(httpservletrequest, "password", "");
if(s1.equals("") || s1 == null)
{
s = s + "You have not input your adminname!<br>";
flag = true;
}
if(s2.equals("") || s2 == null)
{
s = s + "You have not input your password!<br>";
flag = true;
}
if(!flag)
{
DBConnect dbconnect = new DBConnect();
//从库中查找该登陆用户
dbconnect.prepareStatement("SELECT * FROM My_BookAdminuser WHERE adminuser = ? and adminpass = ?");
dbconnect.setString(1, s1);
dbconnect.setString(2, s2);
ResultSet resultset = dbconnect.executeQuery();
if(resultset.next()) //如存在...
{
Admin user = new Admin();
//把从库中读出的记录赋给user
user.SetAdminuser(s1);
user.SetAdminpass(s2);
//把user保存在session中,用户退出时删除
HttpSession httpsession = httpservletrequest.getSession(true);
httpsession.setAttribute("admin", user);
httpsession.setMaxInactiveInterval(3600);
resultset.close();
} else
{
s = s + "Your username or password is error!<br>";
flag = true;
}
dbconnect.close();
}
if(flag)
throw new Exception(s);
else
return;
}
public static void LogoutUser(HttpSession httpsession) throws Exception
{
httpsession.removeAttribute("admin");
}
public static void ChkLogin(HttpSession httpsession) throws Exception
{
boolean flag = false;
String s1 = "";
Admin user = (Admin)httpsession.getAttribute("admin");
if(user==null)
{
s1 = s1 + "You have not loged in!<br>";
flag = true;
}
if(flag)
throw new Exception(s1);
else
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -