首先在数据库中建好一个表。

 

然后创建一个MsterDao类的接口

 1 package com.beiwo.epte.dao;
 2 
 3 import com.beiwo.epte.entity.Master;
 4 
 5 public interface MasterDao extends BaseDao{
 6 
 7     /**
 8      * 保存主人
 9      * @param pet
10      * @return
11      */
12     int save(Master master);
13     
14     
15     /**
16      * 删除主人
17      * @param pet
18      * @return
19      */
20     int del(Master master);
21     
22     /**
23      * 更新主人
24      * @param pet
25      * @return
26      */
27     int update(Master master);
28     
29     /**
30      * 根据Master对象进行查询
31      * @param master
32      * @return
33      */
34     Master findMaster(Master master);
35     
36 }

 

然后创建这个MasterDao的实现类

 1 package com.beiwo.epte.dao.impl;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 
 6 import com.beiwo.epte.dao.MasterDao;
 7 import com.beiwo.epte.entity.Master;
 8 import com.beiwo.epte.util.DBUtils;
 9 import java.sql.ResultSet;
10 
11 public class MasterDaoMySqllmpl extends BaseDaoImpl implements MasterDao {
12 
13     @Override
14     public int save(Master master) {
15         // TODO Auto-generated method stub
16         return 0;
17     }
18 
19     @Override
20     public int del(Master master) {
21         // TODO Auto-generated method stub
22         return 0;
23     }
24 
25     @Override
26     public int update(Master master) {
27         // TODO Auto-generated method stub
28         return 0;
29     }
30 
31     @Override
32     public Master findMaster(Master master) {
33         Master master2 = null;
34 
35         Connection connection = null;
36         PreparedStatement pastmt = null;
37         ResultSet rSet = null;
38 
39         String sql = "SELECT * FROM master WHERE loginId=? AND password=?";
40 
41         try {
42             connection = DBUtils.getConn();
43             pastmt = connection.prepareStatement(sql);
44             pastmt.setString(1, master.getLoginId());
45             pastmt.setString(2, master.getPassword());
46 
47             rSet = pastmt.executeQuery();
48             //指针下移
49             if (rSet.next()) {
50                 master2 = new Master();
51                 master2.setId(rSet.getInt("id"));
52                 master2.setLoginId(rSet.getString("loginId"));
53                 master2.setPassword(rSet.getString("password"));
54 
55             }
56         } catch (Exception e) {
57             // TODO: handle exception
58         } finally {
59             DBUtils.closeAll(rSet, pastmt, connection);
60 
61         }
62 
63         return master2;
64     }
65 
66 }

创建Master的实体类

 1 package com.beiwo.epte.entity;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Master implements Serializable{
 6 
 7     private int id;  //ID
 8     private String loginId;//用户名
 9     private String password;//密码
10     public int getId() {
11         return id;
12     }
13     public void setId(int id) {
14         this.id = id;
15     }
16     public String getLoginId() {
17         return loginId;
18     }
19     public void setLoginId(String loginId) {
20         this.loginId = loginId;
21     }
22     public String getPassword() {
23         return password;
24     }
25     public void setPassword(String password) {
26         this.password = password;
27     }
28     
29 }

 

 

数据库的帮助类

 1 package com.beiwo.epte.util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.Statement;
 7 import java.util.ResourceBundle;
 8 
 9 /**
10  * 数据库的帮助类
11  * 
12  * @author beiwo
13  *
14  */
15 public class DBUtils {
16 
17     private static String driverClass;
18     private static String url;
19     private static String user;
20     private static String password;
21     
22     static{
23         ResourceBundle rb=ResourceBundle.getBundle("jdbc");
24         driverClass=rb.getString("driverClass");
25         url=rb.getString("url");
26         user=rb.getString("user");
27         password=rb.getString("password");
28         
29         try {
30             Class.forName(driverClass);
31         } catch (ClassNotFoundException e) {
32             e.printStackTrace();
33         }
34     }
35     
36 
37     public static Connection getConn() throws Exception {
38         return DriverManager.getConnection(url, user, password);
39     }
40     
41     
42     public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
43         //關閉資源
44         if(null!=rs){
45             try {
46                 rs.close();
47             } catch (Exception e) {
48                 e.printStackTrace();
49             }
50         }
51         if(null!=stmt){
52             try {
53                 stmt.close();
54             } catch (Exception e) {
55                 e.printStackTrace();
56             }
57         }
58         
59         if(null!=conn){
60             try {
61                 conn.close();
62             } catch (Exception e) {
63                 e.printStackTrace();
64             }
65         }
66     }
67     
68     
69     
70 
71 }

 

然后再创建一个接口声明一个登陆的方法

1 package com.beiwo.epte.service;
2 
3 public interface MsterService {
4     
5     //登陆
6     public void login();
7     
8 
9 }

然后创建一个实现类来实现以下这个登陆的方法

 1 package com.beiwo.epte.service.impl;
 2 
 3 import java.util.Scanner;
 4 
 5 import com.beiwo.epte.dao.MasterDao;
 6 import com.beiwo.epte.dao.impl.MasterDaoMySqllmpl;
 7 import com.beiwo.epte.entity.Master;
 8 import com.beiwo.epte.service.MsterService;
 9 
10 public class MasterServiceImpl implements MsterService{
11 
12     Master master = null;
13     @Override
14     public void login() {
15         
16         Scanner scanner = new Scanner(System.in);
17         
18         System.out.println("请输入用户名:");
19         String loginId = scanner.next();
20         
21         System.out.println("请输入密码:");
22         String password = scanner.next();
23         
24         MasterDao masterDao = new MasterDaoMySqllmpl();
25         Master master2 = new Master();
26         master2.setLoginId(loginId);
27         master2.setPassword(password);
28         
29         master=masterDao.findMaster(master2);
30         
31         if (null!=master) {
32             System.out.println("登陆成功");
33         } else {
34             System.out.println("登陆失败");
35         }
36         
37     }
38 }

 

最后创建一个测试类来调用一下这个方法,只要是输入正确都可以登陆成功。

 1 package com.beiwo.epte.test;
 2 
 3 import org.junit.Test;
 4 
 5 import com.beiwo.epte.service.MsterService;
 6 import com.beiwo.epte.service.impl.MasterServiceImpl;
 7 
 8 public class TestMasterDao {
 9     @Test
10     public void testLogin(){
11         MsterService masterService = new MasterServiceImpl();
12         masterService.login();//调用登陆的方法
13         
14     }
15 }

 

posted on 2016-12-14 10:19  栗子先生i  阅读(1805)  评论(0编辑  收藏  举报