程序开发:MVC设计模式与应用(2)

package com.mvc.linuxidc.dbc ;
import java.sql.* ;
/**
 *
 * @author 偶my耶
 *    数据库连接
 */
public class DatabaseConnection {
    private static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
    private static final String DBURL = "jdbc:mysql://localhost:3306/mytest" ;
    private static final String DBUSER = "root" ;
    private static final String DBPASSWORD = "root" ;
    private Connection conn = null ;
    public DatabaseConnection() throws Exception{
        try{
            Class.forName(DBDRIVER) ;
            this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
        }catch(Exception e){
            throw e ;
        }
    }
    public Connection getConnection(){
        return this.conn ;
    }
    public void close() throws Exception{
        if(this.conn != null){
            try{
                this.conn.close() ;
            }catch(Exception e){
                throw e ;
            }
        }
    }

}

package com.mvc.linuxidc.dao ;
import com.mvc.linuxidc.vo.User;
public interface IUserDAO {
   
    public boolean findLogin(User user) throws Exception ;
}

package com.mvc.linuxidc.dao.impl ;

import com.mvc.linuxidc.dao.*;
import com.mvc.linuxidc.vo.User;
import java.sql.* ;
/**
 *
 * @author 偶my耶
 *    Dao实现类
 */
public class UserDAOImpl implements IUserDAO {
    private Connection conn = null ;
    private PreparedStatement pstmt = null ;
    public UserDAOImpl(Connection conn){
        this.conn = conn ;
    }
    public boolean findLogin(User user) throws Exception{
        boolean flag = false ;
        String sql = "SELECT name FROM user WHERE userid=? AND password=?" ;
        this.pstmt = this.conn.prepareStatement(sql) ;
        this.pstmt.setString(1,user.getUserid()) ;
        this.pstmt.setString(2,user.getPassword()) ;
        ResultSet rs = this.pstmt.executeQuery() ;
        if(rs.next()){
            user.setName(rs.getString(1)) ;   
            flag = true ;
        }
        this.pstmt.close() ;
        return flag ;
    }
}

DAOFactory.java

package com.mvc.linuxidc.factory ;
import com.mvc.linuxidc.dao.*;
import com.mvc.linuxidc.dao.proxy.*;
/**
 *
 * @author 偶my耶
 *    工厂接口
 */
public class DAOFactory {
    public static IUserDAO getIUserDAOInstance(){
        return new UserDAOProxy() ;
    }
}

UserDAOProxy.java

package com.mvc.linuxidc.dao.proxy ;
import com.mvc.linuxidc.dbc.DatabaseConnection;
import com.mvc.linuxidc.dao.IUserDAO;
import com.mvc.linuxidc.dao.impl.UserDAOImpl;
import com.mvc.linuxidc.vo.User;
/**
 *
 * @author 偶my耶
 *    工厂类 实现业务操作
 */
public class UserDAOProxy implements IUserDAO {
    private DatabaseConnection dbc = null ;
    private IUserDAO dao = null ;
    public UserDAOProxy(){
        try{
            this.dbc = new DatabaseConnection() ;
        }catch(Exception e){
            e.printStackTrace() ;
        }
        this.dao = new UserDAOImpl(dbc.getConnection()) ;
    }
    public boolean findLogin(User user) throws Exception{
        boolean flag = false ;
        try{
            flag = this.dao.findLogin(user) ;   
        }catch(Exception e){
            throw e ;
        }finally{
            this.dbc.close() ;
        }
        return flag ;
    }
}

LoginServlet.java

package com.mvc.linuxidc.servlet ;
import java.io.* ;
import java.util.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ca0ab2692c45a5bdb32fd9a012b05e30.html