DAO设计模式
DAO设计模式是属于J2EE数据层的操作,使用DAO设计模式可简化代码,增强可移植性。 如果我们直接在JSP页面中写JDBC代码,会导致JSP页面中包含大量的HTML和JSP代码,显示和功能代码混在一起是很难维护的。在JSP页面中使用JDBC代码,必须导入java.sql.*包,这样的设计不是很不合理,因为JSP只需要关注数据的显示,而不需要关注数据从哪里来,怎么来。 1、DAO设计模式,包括5个重要部分。分别为数据库连接类、VO类、DAO接口、DAO实现类及DAO工厂类。 2、数据库连接类:主要功能是连接数据库并获得连接对象,以及关闭数据库。通过数据库连接类可以大大地简化开发,在需要进行数据库连接时,是需要创建该类的实例,并调用其中的方法就可以获得数据库连接对象和关闭数据库。 示例: package com.dao; import java.sql.Connection; import java.sql.DriverManager; public class DBConn { private final String driver = "com.mysql.jdbc.Driver"; private final String url = "jdbc:mysql://localhost:3306/JspTest"; private final String user = "root"; private final String password = "123456"; private Connection conn = null; public DBConn() { try{ Class.forName(driver); this.conn = DriverManager.getConnection(url, user, password); }catch(Exception e){ System.out.println("加载驱动失败!"); } } public Connection getConnection(){ return conn; } public void close(){ try{ conn.close(); }catch(Exception e){ System.out.println("数据库连接关闭失败!"); } } } 3、VO类是一个包含属性和表中字段完全对应的类,并在该类中提供setter和getter方法来设置该类中的属性,类似于JavaBean。 package com.vo; public class User { private int userid; private String username; private String password; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 4、DAO接口:定义了所有的用户的操作,如添加记录、删除记录及查询记录等。仅仅是接口,我们需要实现类。示例: package com.dao; import java.util.List; import com.vo.User; public interface UserDao { public void insert(User user) throws Exception; public void update(User user) throws Exception; public void delete(int userid) throws Exception; public User queryById(int userid) throws Exception; public List queryAll() throws Exception; } 5、DAO类实现: package com.dao; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.vo.User; public class UserDAOImpl implements UserDao { public void insert(User user) throws Exception { String sql = "insert into user(username,password) values(?,?)"; PreparedStatement pstmt = null; DBConn conn = null; try{ conn = new DBConn(); pstmt = conn.getConnection().prepareStatement(sql); pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getPassword()); pstmt.executeUpdate(); pstmt.close(); }catch(Exception e){ throw new Exception("操作出现异常"); }finally{ conn.close(); } } public void update(User user) throws Exception { String sql = "update user set username = ?,password = ? where userid =?"; PreparedStatement pstmt = null; DBConn conn = null; try{ conn = new DBConn(); pstmt = conn.getConnection().prepareStatement(sql); pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getPassword()); pstmt.setInt(3, user.getUserid()); pstmt.executeUpdate(); pstmt.close(); }catch(Exception e){ throw new Exception("操作出现异常"); }finally{ conn.close(); } } public void delete(int userid) throws Exception { String sql = "delete from user where userid =?"; PreparedStatement pstmt = null; DBConn conn = null; try{ conn = new DBConn(); pstmt = conn.getConnection().prepareStatement(sql); pstmt.setInt(1, userid); pstmt.executeUpdate(); pstmt.close(); }catch(Exception e){ throw new Exception("操作出现异常"); }finally{ conn.close(); } } public User queryById(int userid) throws Exception { User user = null; String sql = "select * from user where userid =?"; PreparedStatement pstmt = null; DBConn conn = null; try{ conn = new DBConn(); pstmt = conn.getConnection().prepareStatement(sql); pstmt.setInt(1, userid); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ user = new User(); user.setUserid(rs.getInt(1)); user.setUsername(rs.getString(2)); user.setPassword(rs.getString(3)); } rs.close(); pstmt.close(); }catch(Exception e){ throw new Exception("操作出现异常"); }finally{ conn.close(); } return user; } public List queryAll() throws Exception { List all = new ArrayList(); String sql = "select * from user"; PreparedStatement pstmt = null; DBConn conn = null; try{ conn = new DBConn(); pstmt = conn.getConnection().prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ User user = new User(); user.setUserid(rs.getInt(1)); user.setUsername(rs.getString(2)); user.setPassword(rs.getString(3)); all.add(user); } rs.close(); pstmt.close(); }catch(Exception e){ throw new Exception("操作出现异常"); }finally{ conn.close(); } return all; } } 6、DAO工厂类:在没有DAO工厂类的情况下,必须通过创建DAO实现类的实例才能完成数据库操作。这时就必须知道具体的子类,对于后期的修改非常不便。如果后期需要创建一个操作Oracal的DAO实现类,这时就必须修改所有使用DAO实现类的代码。使用DAO工厂类可以很好地解决后期修改问题,可以通过该DAO工厂类的一个静态方法获得DAO实现类实例。这是如果需要替换DAO实现类,只需修改该DAO工厂类中的方法代码,而不必修改所有的操作数据库代码。 示例: package com.dao; public class DAOFactory { public static UserDao getUserDAOInstance(){ return new Us
转自:http://blog.sina.com.cn/s/blog_70e50f090101lr4t.html