DAO模式
2016-03-12 11:13
211 查看
主要是新建一个interface,然后实例化这个interface,然后对表内的数据封装。最后一个test,四个java如下
customer.java
customerDao.java
customerDaoImpl.java
testDao.java
customer.java
package my_sql_test; public class Customer { //table == 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; } }
customerDao.java
package my_sql_test; import java.util.List; public interface CustomerDao { public void add(Customer C); public void update(Customer C); public void delete(int id); public Custome getCustomerById(int id); public List<Customer> query(); //CRUD }
customerDaoImpl.java
package my_sql_test; 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; public class CustomerDaoImpl implements CustomerDao { @Override public void add(Customer C) { // TODO Auto-generated method stub String sql="insert into customer (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) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtil.close(conn); } } @Override public void update(Customer C) { // TODO Auto-generated method stub String sql="update customer 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) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtil.close(conn); } } @Override public void delete(int id) { // TODO Auto-generated method stub String sql="delete from customer where id=?"; Connection conn=DBUtil.open(); try { PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setInt(1, id); pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtil.close(conn); } } @Override public Custome getCustomerById(int id) { // TODO Auto-generated method stub String sql="select id,name,email from customer 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(2); String email=rs.getString(3); Customer C=new Custome(); C.setId(id); C.setName(name); C.setEmail(email); return C; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtil.close(conn); } return null; } @Override public List<Customer> query() { // TODO Auto-generated method stub String sql="select * from customer"; Connection conn=DBUtil.open(); try { Statement stmt=conn.createStatement(); List<Customer> list=new ArrayList<Custome>(); ResultSet rs=stmt.executeQuery(sql); while(rs.next()){ int id=rs.getInt(1); String name=rs.getString(2); String email=rs.getString(3); Customer C=new Custome(); C.setId(id); C.setName(name); C.setEmail(email); list.add(C); } return list; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ DBUtil.close(conn); } return null; } }
testDao.java
package my_sql_test; import java.util.List; public class testDao { public static void main(String[] args){ CustomerDao dao=new CustomerDaoImpl(); Customer C=new Customer(); Customer c=dao.getCustomerById(2); System.out.println(c); /* List<Customer> list=dao.query(); System.out.println(list); */ // dao.delete(1); /*C.setId(1); C.setName("i"); C.setEmail("ii"); dao.update(C);*/ /*C.setName("tree"); C.setEmail("itree"); dao.add(C);*/ } }
相关文章推荐
- 消息队列函数(msgget、msgctl、msgsnd、msgrcv)及其范例
- linux安全之TCP Wrappers
- 安装epel源后,报错Error: Cannot retrieve metalink for repository: epel. Please verify its path..
- 关于“问吧APP”问卷调查报告分析与体会
- 生成树的计数Matrix-Tree定理 http://blog.csdn.net/longshuai0821/article/details/7764267
- std::map 删除指定条件的元素
- day12—JavaScript基础
- linux 下CA服务器安装
- [国嵌攻略][136][DM9000网卡驱动深度分析]
- 四则运算2——单元测试
- HDU-4277 USACO ORZ
- Android实战简易教程-第六十六枪(服务器端搭建和服务器端Json数据交互)
- 技术活!如何将你的女友修得更性感。
- 建筑抢修(贪心讲题2)
- 2015年大二上-数据结构-内部排序-(4)-快速排序
- linux下tomcat原理讲解
- 二叉树的层序遍历(levelordertraverse)
- 学习列表
- 一种异常处理的方法、装置和软件系统
- 隔离级别(Isolation Level)