package userce;
import java.sql.*;
import java.io.*;
import java.util.*;
public class UserCheck {
Connection con;
ResultSet rs;
public UserCheck() { }
public Connection getConnect(){ //連接數(shù)據(jù)庫(kù)的,不用在多說(shuō)了吧
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e){}
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=flDataSource";
String name = "sa";//建議設(shè)計(jì)數(shù)據(jù)庫(kù)時(shí),不要用默認(rèn)的sa,可以建立一個(gè)有操作權(quán)限的用戶(hù);
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();//關(guān)于this 的用法,我到現(xiàn)在理解的也不是太透徹,我這樣用,在實(shí)際操作中是通過(guò)的,如果有不妥之處,請(qǐng)高手指教.
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){//此函數(shù)用來(lái)判斷是否有此用戶(hù),其實(shí)很好理解我定義成boolean型,就可以根據(jù)返回值來(lái)進(jìn)行一個(gè)<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){ //次方法我用來(lái)根據(jù)傳入的參數(shù):username(我設(shè)置session時(shí),用的也是username,根據(jù)檢索數(shù)據(jù)庫(kù)中的0,1標(biāo)志位,來(lái)判斷用戶(hù)的權(quán)限,這樣就可以進(jìn)行相應(yīng)的操作.)
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;
}
}