首先在数据库中建好一个表。
然后创建一个MsterDao类的接口
package com.beiwo.epte.dao; import com.beiwo.epte.entity.Master; public interface MasterDao extends BaseDao{ /**
* 保存主人
* @param pet
* @return
*/
int save(Master master); /**
* 删除主人
* @param pet
* @return
*/
int del(Master master); /**
* 更新主人
* @param pet
* @return
*/
int update(Master master); /**
* 根据Master对象进行查询
* @param master
* @return
*/
Master findMaster(Master master); }
然后创建这个MasterDao的实现类
package com.beiwo.epte.dao.impl; import java.sql.Connection;
import java.sql.PreparedStatement; import com.beiwo.epte.dao.MasterDao;
import com.beiwo.epte.entity.Master;
import com.beiwo.epte.util.DBUtils;
import java.sql.ResultSet; public class MasterDaoMySqllmpl extends BaseDaoImpl implements MasterDao { @Override
public int save(Master master) {
// TODO Auto-generated method stub
return 0;
} @Override
public int del(Master master) {
// TODO Auto-generated method stub
return 0;
} @Override
public int update(Master master) {
// TODO Auto-generated method stub
return 0;
} @Override
public Master findMaster(Master master) {
Master master2 = null; Connection connection = null;
PreparedStatement pastmt = null;
ResultSet rSet = null; String sql = "SELECT * FROM master WHERE loginId=? AND password=?"; try {
connection = DBUtils.getConn();
pastmt = connection.prepareStatement(sql);
pastmt.setString(1, master.getLoginId());
pastmt.setString(2, master.getPassword()); rSet = pastmt.executeQuery();
//指针下移
if (rSet.next()) {
master2 = new Master();
master2.setId(rSet.getInt("id"));
master2.setLoginId(rSet.getString("loginId"));
master2.setPassword(rSet.getString("password")); }
} catch (Exception e) {
// TODO: handle exception
} finally {
DBUtils.closeAll(rSet, pastmt, connection); } return master2;
} }
创建Master的实体类
package com.beiwo.epte.entity; import java.io.Serializable; public class Master implements Serializable{ private int id; //ID
private String loginId;//用户名
private String password;//密码
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }
数据库的帮助类
package com.beiwo.epte.util; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ResourceBundle; /**
* 数据库的帮助类
*
* @author beiwo
*
*/
public class DBUtils { private static String driverClass;
private static String url;
private static String user;
private static String password; static{
ResourceBundle rb=ResourceBundle.getBundle("jdbc");
driverClass=rb.getString("driverClass");
url=rb.getString("url");
user=rb.getString("user");
password=rb.getString("password"); try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public static Connection getConn() throws Exception {
return DriverManager.getConnection(url, user, password);
} public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
//關閉資源
if(null!=rs){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(null!=stmt){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
} if(null!=conn){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} }
然后再创建一个接口声明一个登陆的方法
package com.beiwo.epte.service; public interface MsterService { //登陆
public void login(); }
然后创建一个实现类来实现以下这个登陆的方法
package com.beiwo.epte.service.impl; import java.util.Scanner; import com.beiwo.epte.dao.MasterDao;
import com.beiwo.epte.dao.impl.MasterDaoMySqllmpl;
import com.beiwo.epte.entity.Master;
import com.beiwo.epte.service.MsterService; public class MasterServiceImpl implements MsterService{ Master master = null;
@Override
public void login() { Scanner scanner = new Scanner(System.in); System.out.println("请输入用户名:");
String loginId = scanner.next(); System.out.println("请输入密码:");
String password = scanner.next(); MasterDao masterDao = new MasterDaoMySqllmpl();
Master master2 = new Master();
master2.setLoginId(loginId);
master2.setPassword(password); master=masterDao.findMaster(master2); if (null!=master) {
System.out.println("登陆成功");
} else {
System.out.println("登陆失败");
} }
}
最后创建一个测试类来调用一下这个方法,只要是输入正确都可以登陆成功。
package com.beiwo.epte.test; import org.junit.Test; import com.beiwo.epte.service.MsterService;
import com.beiwo.epte.service.impl.MasterServiceImpl; public class TestMasterDao {
@Test
public void testLogin(){
MsterService masterService = new MasterServiceImpl();
masterService.login();//调用登陆的方法 }
}