课时8:DAO设计模式
2015-11-05 15:36
393 查看
1.DAO简介
DAO的全称是:Data Access Object,数据访问对象。
使用DAO设计模式,来封装数据库持久的的所有操作(CRUD),使低级的数据逻辑和高级的业务逻辑分离,达到解耦合的目的。
2.一个典型的DAO实现有如下的组件:
一个DAO接口
数据传输对象(有时称为值对象)
一个实现了DAO接口的具体类
一个DAO工厂类
3.以维护一个客户信息为例,具体组件如下所示:
CustomerDao 解耦
Customer 值对象(VO)
CustomerDaoImpl(接口的具体实现类)
CustomerFactory(工厂类,实例化用)
4.实例
Customer Bean类
1.建立一个类class Customer,里面存放所有数据库内成员的信息
2.建立一个接口interface CustomerDao,里面存放对数据库操作的所有方法
3.建立一个类class CustomerDaoImpl implements CustomerDao,并且实现CustomerDao里面的所有方法
4.建立一个类class Test6,对数据库进行操作
![](http://blog.csdn.net/bin8632/article/details/file:/C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/af4bbf8d8305444197d8023ae816d250/attachment.png)
package com.geek99.demo;
//这张表是数据库里的内容一一对应
public class Customer {
private int id;
private String name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString(){
return id+":"+name+":"+email;
}
}
![](http://blog.csdn.net/bin8632/article/details/file:/C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/8fefb8173f47471dbb0e53169f888a42/attachment.png)
package com.geek99.demo;
import java.util.List;
public interface CustomerDao {
public void add(Customer c);
public void update(Customer c);
public void delete(int id);
public Customer getCustomerById(int id);
public List<Customer> query();
//CRUD create retrieval update delete
}
![](http://blog.csdn.net/bin8632/article/details/file:/C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/966dc1515caa41d3883b6ab82af86a45/attachment.png)
package com.geek99.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
//Customer javabean
//CustomerDao
//CustomerDaoImpl
public class CustomerDaoImpl implements CustomerDao{
public void add(Customer c) {
String sql="insert into CustomerTbl(name,email)values(?,?)";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,c.getName());
pstmt.setString(2, c.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
public void update(Customer c) {
String sql="update CustomerTbl set name=?,email=? where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,c.getName());
pstmt.setString(2, c.getEmail());
pstmt.setInt(3, c.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
public void delete(int id) {
String sql="delete from CustomerTbl where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
public Customer getCustomerById(int id) {
String sql="select id,name,email from CustomerTbl where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
String name=rs.getString(1);
String email=rs.getString(2);
Customer c=new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
return c;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return null;
}
public List<Customer> query() {
String sql="select id,name,email from CustomerTbl";
Connection conn=DBUtil.open();
try {
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
List<Customer> list=new ArrayList<Customer>();
while(rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
String email=rs.getString(3);
Customer c=new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
list.add(c);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return null;
}
}
![](http://blog.csdn.net/bin8632/article/details/file:/C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/a32a6718d2034c9097da552c0d163b79/attachment.png)
package com.geek99.demo;
import java.util.List;
public class Test6 {
public static void main(String[] args) {
CustomerDao dao=new CustomerDaoImpl();
// Customer c=new Customer();
// c.setName("geek");
// c.setEmail("geek@126.com");
// dao.add(c);
// c.setId(11);
// c.setName("newgeek");
// c.setEmail("newgeek@111.com");
// dao.update(c);
// dao.delete(10);
// List<Customer> list=dao.query();
// System.out.println(list);
Customer c=dao.getCustomerById(11);
System.out.println(c);
}
}
DAO的全称是:Data Access Object,数据访问对象。
使用DAO设计模式,来封装数据库持久的的所有操作(CRUD),使低级的数据逻辑和高级的业务逻辑分离,达到解耦合的目的。
2.一个典型的DAO实现有如下的组件:
一个DAO接口
数据传输对象(有时称为值对象)
一个实现了DAO接口的具体类
一个DAO工厂类
3.以维护一个客户信息为例,具体组件如下所示:
CustomerDao 解耦
Customer 值对象(VO)
CustomerDaoImpl(接口的具体实现类)
CustomerFactory(工厂类,实例化用)
4.实例
Customer Bean类
1.建立一个类class Customer,里面存放所有数据库内成员的信息
2.建立一个接口interface CustomerDao,里面存放对数据库操作的所有方法
3.建立一个类class CustomerDaoImpl implements CustomerDao,并且实现CustomerDao里面的所有方法
4.建立一个类class Test6,对数据库进行操作
![](http://blog.csdn.net/bin8632/article/details/file:/C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/af4bbf8d8305444197d8023ae816d250/attachment.png)
package com.geek99.demo;
//这张表是数据库里的内容一一对应
public class Customer {
private int id;
private String name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString(){
return id+":"+name+":"+email;
}
}
![](http://blog.csdn.net/bin8632/article/details/file:/C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/8fefb8173f47471dbb0e53169f888a42/attachment.png)
package com.geek99.demo;
import java.util.List;
public interface CustomerDao {
public void add(Customer c);
public void update(Customer c);
public void delete(int id);
public Customer getCustomerById(int id);
public List<Customer> query();
//CRUD create retrieval update delete
}
![](http://blog.csdn.net/bin8632/article/details/file:/C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/966dc1515caa41d3883b6ab82af86a45/attachment.png)
package com.geek99.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
//Customer javabean
//CustomerDao
//CustomerDaoImpl
public class CustomerDaoImpl implements CustomerDao{
public void add(Customer c) {
String sql="insert into CustomerTbl(name,email)values(?,?)";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,c.getName());
pstmt.setString(2, c.getEmail());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
public void update(Customer c) {
String sql="update CustomerTbl set name=?,email=? where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,c.getName());
pstmt.setString(2, c.getEmail());
pstmt.setInt(3, c.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
public void delete(int id) {
String sql="delete from CustomerTbl where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
}
public Customer getCustomerById(int id) {
String sql="select id,name,email from CustomerTbl where id=?";
Connection conn=DBUtil.open();
try {
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
String name=rs.getString(1);
String email=rs.getString(2);
Customer c=new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
return c;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return null;
}
public List<Customer> query() {
String sql="select id,name,email from CustomerTbl";
Connection conn=DBUtil.open();
try {
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
List<Customer> list=new ArrayList<Customer>();
while(rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
String email=rs.getString(3);
Customer c=new Customer();
c.setId(id);
c.setName(name);
c.setEmail(email);
list.add(c);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(conn);
}
return null;
}
}
![](http://blog.csdn.net/bin8632/article/details/file:/C:/Users/bill/AppData/Local/YNote/data/bin890607@126.com/a32a6718d2034c9097da552c0d163b79/attachment.png)
package com.geek99.demo;
import java.util.List;
public class Test6 {
public static void main(String[] args) {
CustomerDao dao=new CustomerDaoImpl();
// Customer c=new Customer();
// c.setName("geek");
// c.setEmail("geek@126.com");
// dao.add(c);
// c.setId(11);
// c.setName("newgeek");
// c.setEmail("newgeek@111.com");
// dao.update(c);
// dao.delete(10);
// List<Customer> list=dao.query();
// System.out.println(list);
Customer c=dao.getCustomerById(11);
System.out.println(c);
}
}
相关文章推荐
- c++字符集之间转换(UTF-8,UNICODE,Gb2312)
- AFN-上传文件
- 打印 保存
- sublime text 3 快捷键大全以及配置编译环境
- ECharts+百度地图网络拓扑图应用
- js如何实现淡入淡出效果
- Android面试,IntentService的原理及使用
- java常用库学习(3)
- mysql 重置 root 密码
- 测试基础篇之【按钮通用测试点】
- 运用上一节介绍的类加载的过程,介绍一个详细的实例
- 注册 登录
- 【转】SIP 中的Dialog,call,session 和 transaction
- 自定义控件其实很简单 二
- otter
- 【详解】Python统一解密改进版
- cp 拷贝文件保持源文件的时间属性
- Mozilla使用开源项目清单
- java 中String的创建产生的对象
- 译:PendingIntent详解