Struts2(驗證登陸用戶 DB:mysql)

1. 首先要下載Connector/J地址:http://www.mysql.com/downloads/connector/j/

這是MySQL官方提供的連接方式:

解壓後得到jar庫文件,需要在工程中導入該庫文件


2. 先寫一個class來連接數據庫

[java] view plain copy
  1. public class dbconnector {  
  2.     public static final String url = "jdbc:mysql://127.0.0.1/test";  
  3.     public static final String name = "com.mysql.jdbc.Driver";  
  4.     public static final String user = "root";  
  5.     public static final String password = "";  
  6.   
  7.     public Connection conn = null;  
  8.     public PreparedStatement pst = null;  
  9.   
  10.     public dbconnector(String sql) {  
  11.         try {  
  12.             Class.forName(name);// 指定連接類型  
  13.             conn = DriverManager.getConnection(url, user, password);// 獲取連接  
  14.             pst = conn.prepareStatement(sql);// 準備執行語句  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
  19.   
  20.     public void close() {  
  21.         try {  
  22.             this.conn.close();  
  23.             this.pst.close();  
  24.         } catch (SQLException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28.   
  29. }  


3. 在Dao類中執行

[java] view plain copy
  1. public String select(User user) {  
  2.         Map<String, String> usersMap = new HashMap<String, String>();    
  3.         String sql = null;    
  4.         dbconnector db1 = null;    
  5.         ResultSet ret = null;    
  6.           
  7.         try {    
  8.             sql = "select * from user";//SQL語句    
  9.             db1 = new dbconnector(sql);  
  10.             ret = db1.pst.executeQuery();//執行語句,得到結果集    
  11.             while (ret.next()) {    
  12.                 String uid = ret.getString(1);    
  13.                 String uname = ret.getString(2);    
  14.                 String upassword = ret.getString(3);      
  15.                 usersMap.put(uname,upassword);  
  16.             }//顯示數據    
  17.               
  18.             ret.close();    
  19.             db1.close();//關閉連接     
  20.     
  21.             // 傳入的用戶的用戶名與密碼    
  22.             String userName = user.getUsername();    
  23.             String password = user.getPassword();    
  24.     
  25.             if (usersMap.containsKey(userName)    
  26.                     && usersMap.get(userName).equals(password)) {    
  27.                 return "1";    
  28.             } else {    
  29.                 return "0";    
  30.             }    
  31.         } catch (Exception e) {    
  32.             e.printStackTrace();    
  33.         }    
  34.         return null;    
  35.     }    


4. 可以在執行類裏寫一個main函數來測試

[java] view plain copy
  1. public static void main(String[] args) {  
  2.         UserDaoJDBC udj = new UserDaoJDBC();  
  3.         User u = new User();  
  4.         u.setUsername("tester01");  
  5.         u.setPassword("tester");  
  6.         System.out.println(udj.select(u));  
  7.     }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章