`
miracle9i
  • 浏览: 34781 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

DAO设计模式

阅读更多

java中的DAO模式是一个重要的概念,下面以一个登陆程序的例子,结合地理解DAO模式。

DAO模式的主要构成有:

1. PO包
   PO对象,用于存储映射数据库表的对象
package i9.m.po ;
// 值对象,包含属性,setter,getter方法
public class Person
...{
    
private String name ;
    
private String password ;

    
// 生成getter、setter方法
    public void setName(String name)
    
...{
        
this.name = name ;
    }

    
public void setPassword(String password)
    
...{
        
this.password = password ;
    }

    
public String getName()
    
...{
        
return this.name ;
    }

    
public String getPassword()
    
...{
        
return this.password ;
    }

这里假定传入的只有name和password两个属性,po实质上就是一个简单的JAVABEAN

2.DAO包
(1)一个connection类:管理数据库的连接对象:
package i9.m.dao;
import java.sql.*;

public class DBConnection ...{
    
private final String driver="com.mysql.jdbc.Driver";
    
private final String url="jdbc:mysql://localhost:3306/newsdata";
    
private final String user="root";
    
private final String pwd="root";
    
    
private Connection conn;
    
    
public DBConnection()
    
...{
            
try ...{
                Class.forName(driver);
                
this.conn =  DriverManager.getConnection(url,user,pwd);
            }
 catch (ClassNotFoundException e) ...{
                
// TODO 自动生成 catch 块
                e.printStackTrace();
            }
 catch (SQLException e) ...{
                
// TODO 自动生成 catch 块
                e.printStackTrace();
            }
    
        
    }

    
    
public Connection getConnection()
    
...{
        
return this.conn;
    }

    
    
public void close()
    
...{
        
try ...{
            
this.conn.close();
        }
 catch (SQLException e) ...{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

    }

}
(2)一个接口:规定对特定的数据的存取接口
package i9.m.dao;
import i9.m.po.*;
import java.util.*;

public interface IPersonDAO ...{
    
    
public void insert(Person person) throws Exception;
    
    
public void update(Person person)throws Exception;
    
    
public List queryAll() throws Exception ;
    
    
public Person queryByID(String id) throws Exception;
    
    
public void delete(String id)throws Exception;
}
(3)一个接口的实现类:实现具体的数据库存取操作
/** *//**design by Miracle9i @2007-11-13
 * 
 
*/

package i9.m.dao;
import java.util.*;
import java.sql.*;
import i9.m.po.*;

/** *//**
 * design by Miracle9i @2007-11-13
 *
 
*/

public class PersonDAOImple implements IPersonDAO...{
    
    
public void insert(Person person) throws Exception
    
...{
        String name 
= person.getName();
        String pwd 
= person.getPassword();
        PreparedStatement pstmt 
= null;
        DBConnection dbc 
= null;
        String sql 
= "insert into admins (id,username,password) values (?,?,?)";
        
        
try ...{
            dbc 
= new DBConnection();
            pstmt 
= dbc.getConnection().prepareStatement(sql);
            pstmt.setString(
1, name);
            pstmt.setString(
2, pwd);
            pstmt.executeUpdate();
        }
 catch (RuntimeException e) ...{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
finally
        
...{
            pstmt.close();
        }

        
        
    }

    
    
public void update(Person person)throws Exception
    
...{
        String name 
= person.getName();
        String pwd 
= person.getPassword();
        PreparedStatement pstmt 
= null;
        DBConnection dbc 
= null;
        String sql 
= "update admins set id=?,username=?,password=?";
        
        
try ...{
            dbc 
= new DBConnection();
            pstmt 
= dbc.getConnection().prepareStatement(sql);
            pstmt.setString(
1, name);
            pstmt.setString(
2, pwd);
            pstmt.executeUpdate();
        }
 catch (RuntimeException e) ...{
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
finally
        
...{
            pstmt.close();
        }


        
    }

    
    
public List queryAll() throws Exception 
</di>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics