首先在數據庫中建好一個表。java
而後建立一個MsterDao類的接口sql
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的實體類ide
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 }
而後再建立一個接口聲明一個登錄的方法this
1 package com.beiwo.epte.service; 2 3 public interface MsterService { 4 5 //登錄 6 public void login(); 7 8 9 }
而後建立一個實現類來實現如下這個登錄的方法url
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 }
最後建立一個測試類來調用一下這個方法,只要是輸入正確均可以登錄成功。spa
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 }