【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-UserMan.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package com.csbook.documentsystem;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
public class UserMan
{
Context ctx=null;
DataSource ds=null;
SysLog log=null;
//构造函数
public UserMan()
{
//从连接池中获取数据库连接
try{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("documents");
}
catch(NamingException e){
e.printStackTrace();
}
//建立一个日志类的实例
log=new SysLog();
}
//删除用户
//operator为执行删除操作的人员,userId为要删除的用户ID
public void removeUser(String operator,String userId){
Connection con = null;
PreparedStatement ps = null;
try {
String sqlupdate = "delete from users where id=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlupdate);
ps.setString(1,userId);
ps.executeUpdate();
log.addLog(operator,"remove user"+userId,"users");
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
}
//检查指定的用户是否存在
//userId为要检查的用户ID
public boolean userExist(String userId){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
boolean occupied=true;
try{
String sqlquery="select * from users where id=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlquery);
ps.setString(1,userId);
rs=ps.executeQuery();
if(!rs.next())
occupied=false;
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return occupied;
}
//验证用户是否为合法用户
//user为用户ID,pwd为
登陆用户提交的密码
public boolean isValidUser(String user,String pwd)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
boolean isValid=false;
try{
String sqlquery="select * from users where id=? and pass
word=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlquery);
ps.setString(1,user);
ps.setString(2,pwd);
rs=ps.executeQuery();
if(rs.next())
isValid=true;
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return isValid;
}
//添加用户
//operator为执行添加的操作员,userId为添加的用户ID,pri指定所添加的用户的种类(1为管理员,0为普通用户)
public void addUser(String operator,String userId, int pri){
Connection con = null;
PreparedStatement ps = null;
try{
String sqlInsert = "insert into users(id,privilege) values(?,?)";
con=ds.getConnection();
ps=con.prepareStatement(
sqlInsert);
ps.setString(1,userId);
ps.setInt(2,pri);
ps.executeUpdate();
log.addLog(operator,"add user "+userId,"user");
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
}
//获取用户的种类(特权级别)
//user为指定的用户
public int getUserPri(String user)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
int pri=0;
try{
String sqlquery="select privilege from users where id=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlquery);
ps.setString(1,user);
rs=ps.executeQuery();
if(rs.next())
pri=rs.getInt("privilege");
}
catch(SQLException e){
e.printStackTrace();
}
finally{
if (rs != null) try {rs.close();}
catch (SQLException ignore) {}
if (ps != null) try {ps.close();}
catch (SQLException ignore) {}
if (con != null) try {con.close();}
catch (SQLException ignore) {}
}
return pri;
}
//检查用户是否为系统中某个档案库的管理员,如"是",返回true,否则,返回false
//user为指定的用户
public boolean isAdmin(String user)
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs=null;
boolean admin=false;
try{
String sqlQuery="select * from docBase where admin=?";
con=ds.getConnection();
ps=con.prepareStatement(sqlQuery);
ps.setSt