entity(实体类):
/**
*seralizable序列化
*
/
public class Mas implements Serializable{
/**
* 私有化属性
*/
private static final long serialVersionUID = 1L;
private int id;
private String loginid;
private String password;
public Mas(){
}
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;
}
}
utils(帮助类)
public class BaseDao {
public Connection getConnection(){
ResultSet rs=null;
PreparedStatement pst=null;
Connection conn=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databasename=epet";
String uname = "sa";
String pwd = "123456";
conn = DriverManager.getConnection(url,uname,pwd);
} catch (ClassNotFoundException ee) {
ee.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/* * 修改,删除,增加 */
public void exectue(String sql,Object [] param){
ResultSet rs=null;
PreparedStatement pst=null;
Connection conn=null;
try {
conn=getConnection();
pst=conn.prepareStatement(sql);
for(int i=0;i<param.length;i++){
pst.setObject(i+1, param[i]);
}
int num=pst.executeUpdate();
} catch (Exception ee) {
ee.printStackTrace();
}
}
/*
* 关闭数据库连接
* param conn 数据库连接
* param stmt Statement 对象
* param rs 结果集
*
*/
public void close(ResultSet rs, PreparedStatement pst, Connection conn) {
try{
if(rs!=null){
rs.close();
}
if(pst!=null){
pst.close();
}
if(conn!=null){
conn.close();
}
}catch(SQLException eee){
eee.printStackTrace();
}
}
}
Dao层
/*
* 接口
*/
public interface MasDao {
/*
* 登录
* @param m 注入对象 :主要是id和密码
* @return 主人对象
*/
public Mas login(Mas m);
}
/*
* 继承帮助类实现接口
*/
public class MasDaoImpl extends BaseDao implements MasDao {
public Mas login(Mas m) {
PreparedStatement pst =null;
ResultSet rs=null;
Connection conn=getConnection();
Mas ms=null;
String sql="select * from mas where loginid=? and password=?";
try{
pst=conn.prepareStatement(sql);
pst.setString(1, m.getLoginid());
pst.setString(2, m.getPassword());
rs=pst.executeQuery();
while(rs.next()){
ms=new Mas();
ms.setId(rs.getInt("id"));
ms.setLoginid(rs.getString("loginid"));
ms.setPassword(rs.getString("password"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
close(rs,pst,conn);
}
return m;
}
}
biz层
/*
* 接口
*/
public interface MasBiz {
/*
* 登陆
*/
public Mas login(Mas m);
}
/*
* 实现接口
*/
public class MasBizlmpl implements MasBiz{
MasDao dao=new MasDaoImpl();
//业务层:简单业务,一个sql
public Mas login(Mas m){
//访问层
Mas mm=dao.login(m);
return mm;
}
}
Controller(控制层)<web>
public class MasController {
//页面跳转,数据封装
public Mas login(String loginId ,String password){
MasBiz mb = new MasBizlmpl();
Mas m=new Mas();
m.setLoginid(loginId);
m.setPassword(password);
Mas mm=mb.login(m);
return mm;
}