L遷客

          技術(shù)博客
          隨筆 - 1, 文章 - 12, 評論 - 1, 引用 - 0
          數(shù)據(jù)加載中……

          一個簡單的關(guān)于權(quán)限判斷登陸的javaBean文件

          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ù)庫的,不用在多說了吧
             try{
                  Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
                  }
             catch(ClassNotFoundException e){}
                  String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=flDataSource";
                  String name = "sa";//建議設(shè)計數(shù)據(jù)庫時,不要用默認的sa,可以建立一個有操作權(quán)限的用戶;
                  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)在理解的也不是太透徹,我這樣用,在實際操作中是通過的,如果有不妥之處,請高手指教.
                 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ù)用來判斷是否有此用戶,其實很好理解我定義成boolean型,就可以根據(jù)返回值來進行一個<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){  //次方法我用來根據(jù)傳入的參數(shù):username(我設(shè)置session時,用的也是username,根據(jù)檢索數(shù)據(jù)庫中的0,1標(biāo)志位,來判斷用戶的權(quá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;
            }
          }

          posted on 2012-05-26 20:06 L遷客 閱讀(1637) 評論(1)  編輯  收藏

          評論

          # re: 一個簡單的關(guān)于權(quán)限判斷登陸的javaBean文件  回復(fù)  更多評論   

          其實應(yīng)該在檢查用戶是否存在的時候返回一個integer -1不存在,0密碼錯誤 1 通過。
          檢查的時候用用戶名查詢 查詢到0條表示不存在,1條表示存在,1+條表示系統(tǒng)錯誤。 1的時候再equals一下passWord
          2012-05-27 20:33 | 葉知泉

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 砀山县| 米林县| 潜山县| 两当县| 全椒县| 河西区| 蒙山县| 孝义市| 剑河县| 时尚| 中方县| 曲靖市| 尼木县| 元朗区| 晋中市| 新余市| 延吉市| 易门县| 郁南县| 宽甸| 织金县| 江陵县| 夏邑县| 太谷县| 汕尾市| 宝山区| 马公市| 文昌市| 延吉市| 台江县| 黔南| 竹溪县| 华池县| 巴林右旗| 体育| 南和县| 镇江市| 红安县| 长沙市| 水富县| 綦江县|