一个简单的关于权限判断登陆的javaBean文件
2006-07-06 02:30
597 查看
package userce;
import java.sql.*;
import java.io.*;
import java.util.*;
public class UserCheck {
Connection con;
ResultSet rs;
public UserCheck() { }
public Connection getConnect(){ //连接数据库的,不用在多说了吧
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e){}
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=flDataSource";
String name = "sa";//建议设计数据库时,不要用默认的sa,可以建立一个有操作权限的用户;
String pass = "sa";
try{
con = DriverManager.getConnection(url,name,pass);
}
catch(SQLException e){}
return con;
}
public boolean userExist(String username){
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean occupied=true;
try{
String sqlquery="select *from Userlist where username=?";
con=this.getConnect();
//this.getConnect()=getConnect();//关于this 的用法,我到现在理解的也不是太透彻,我这样用,在实际操作中是通过的,如果有不妥之处,请高手指教.
ps=con.prepareStatement(sqlquery);
ps.setString(2,username);
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;
}
public boolean isValidUser(String username,String userpwd){//此函数用来判断是否有此用户,其实很好理解我定义成boolean型,就可以根据返回值来进行一个<jsp:forword="mmm.jsp">.
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean isValid=false;
try{
String sqlquery="select *from Userlist where username=? and userpwd=?";
con=this.getConnect();
ps=con.prepareStatement(sqlquery);
ps.setString(1,username);
ps.setString(2,userpwd);
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;
}
public int getUserPri(String username){ //次方法我用来根据传入的参数:username(我设置session时,用的也是username,根据检索数据库中的0,1标志位,来判断用户的权限,这样就可以进行相应的操作.)
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
int pri=0;
try{
String sqlquery="select variety from Userlist where username=? ";
con=this.getConnect();
ps=con.prepareStatement(sqlquery);
ps.setString(1,username);
rs=ps.executeQuery();
if(rs.next())
pri=rs.getInt("variety");
}
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;
}
}
import java.sql.*;
import java.io.*;
import java.util.*;
public class UserCheck {
Connection con;
ResultSet rs;
public UserCheck() { }
public Connection getConnect(){ //连接数据库的,不用在多说了吧
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e){}
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=flDataSource";
String name = "sa";//建议设计数据库时,不要用默认的sa,可以建立一个有操作权限的用户;
String pass = "sa";
try{
con = DriverManager.getConnection(url,name,pass);
}
catch(SQLException e){}
return con;
}
public boolean userExist(String username){
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean occupied=true;
try{
String sqlquery="select *from Userlist where username=?";
con=this.getConnect();
//this.getConnect()=getConnect();//关于this 的用法,我到现在理解的也不是太透彻,我这样用,在实际操作中是通过的,如果有不妥之处,请高手指教.
ps=con.prepareStatement(sqlquery);
ps.setString(2,username);
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;
}
public boolean isValidUser(String username,String userpwd){//此函数用来判断是否有此用户,其实很好理解我定义成boolean型,就可以根据返回值来进行一个<jsp:forword="mmm.jsp">.
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean isValid=false;
try{
String sqlquery="select *from Userlist where username=? and userpwd=?";
con=this.getConnect();
ps=con.prepareStatement(sqlquery);
ps.setString(1,username);
ps.setString(2,userpwd);
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;
}
public int getUserPri(String username){ //次方法我用来根据传入的参数:username(我设置session时,用的也是username,根据检索数据库中的0,1标志位,来判断用户的权限,这样就可以进行相应的操作.)
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
int pri=0;
try{
String sqlquery="select variety from Userlist where username=? ";
con=this.getConnect();
ps=con.prepareStatement(sqlquery);
ps.setString(1,username);
rs=ps.executeQuery();
if(rs.next())
pri=rs.getInt("variety");
}
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;
}
}
相关文章推荐
- 一个简单的关于权限判断登陆的javaBean文件
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 创建文件
- 简单了解Java本身是怎样判断一个文件是目录
- Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)
- 关于MMC不能打开文件C:\Program Files\Microsoft SQL Server\80\Tools\Binn\SQL Server Enterprise Manager.MSC可能是由于文件不存在,不是一个MMC控制台,或者用后来的MMC版本创建。也可能你没有访问此文件的足够权限
- 判断是否登陆然后从服务器下载一个文件
- java关于读取数据后简单写入一个文件中的总结
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 读取文件
- 关于MMC不能打开文件C:\Program Files\Microsoft SQL Server\80\Tools\Binn\SQL Server Enterprise Manager.MSC可能是由于文件不存在,不是一个MMC控制台,或者用后来的MMC版本创建。也可能你没有访问此文件的足够权限
- 关于CentOS系统中,文件权限第11位上是一个点的解读
- 【转载】关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 创建文件
- 关于简单的三层的简化(bll,dal,model)的封装这里全部都在一个文件主要在于明白意思
- 关于大小端的简单介绍以及使用C写一个函数判断当前系统是大端存储还是小端存储
- 关于JAVA中如何判断一个文件…
- 创建一个简单的权限判断Access类
- 关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 高级操作
- 一个文件操作的简单程序:关于文件的fopen、fread、fseek、ftell、fclose等操作(FIFO related)
- 【Linux系统 简单分配权限】以root的身份 分配 某用户 的一个文件权限
- 【转载】关于:读写xls文件,提供下载的一个小例子(jxl 的简单运用) - 读取文件
- 一个文件操作的简单程序:关于文件的fopen、fread、fseek、ftell、fclose等操作(FIFO related)