DAO

dao
 1)什么是dao
 data access object(数据访问对象),
 dao封装了数据访问逻辑,使得调用者不用关心
 具体的数据访问技术就可以访问数据库或者其它的
 存储设备(比如文件、目录服务器、或者是其它的
 第三方的程序)。
 2)dao的组成
  a,实体
  一个java类,这个类与数据库中的表对应。
   比如,t_order表与Order类对应:
    对应关系指的是:
    t_order表名与Order类名对应
    t_order表的列与Order类的属性对应
    t_order表中的一条记录与Order
    类的一个实例对应
  b,dao接口
   声明一系列方法(即对数据库进行哪些操作),
  这些方法应该与具体的技术无关。
  c,dao实现
   实现dao接口的一个具体类
  d,工厂
   提供符合接口定义的对象,调用者不用关心
   对象的创建细节。
   也就是说,通过工厂,可以将调用者与要调用的
   对象解耦了。

Config.properties代码
StudentDAO=dao.impl.StudentDAOJdbcImpl 
Configutil.java代码
    package util;  
      
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.util.Properties;  
      
    public class ConfigUtil {  
        private static Properties props = new Properties();  
        static {  
            InputStream is = ConfigUtil.class.getClassLoader().getResourceAsStream(  
                    "test/config.properties");  
            try {  
                props.load(is);  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
      
        public static String getValue(String key) {  
            return props.getProperty(key);  
        }  
      
        public static void main(String[] args) {  
            System.out.println(getValue("StudentDAO"));  
        }  
    }  
Factory.java代码
    package util;  
      
    public class Factory {  
        public static Object getInstance(String type) {  
            String className = ConfigUtil.getValue(type);  
            Object obj = null;  
            try {  
                obj = Class.forName(className).newInstance();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            return obj;  
        }  
    }  

 

 

Studentdao代码
    package dao;  
      
    import java.util.List;  
      
    import entity.Student;  
      
    public interface StudentDAO {  
        public List<Student> list() throws Exception;  
      
        public void add(Student s) throws Exception;  
      
        public void delete(long id) throws Exception;  
      
        public Student find(long id) throws Exception;  
      
        public void update(Student s) throws Exception;  
    }  

 

Studentdaojdbcimpl.java代码
package dao.impl;  
  
import java.sql.Connection;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.sql.Statement;  
import java.util.ArrayList;  
import java.util.List;  
  
import util.DBUtil;  
  
import dao.StudentDAO;  
import entity.Student;  
  
public class StudentDAOJdbcImpl implements StudentDAO {  
  
    public void add(Student s) throws Exception {  
        Connection conn = DBUtil.getConnection();  
        PreparedStatement ps = conn  
                .prepareStatement("insert into t_student(stuNum,name,sex,address) values(?,?,?,?)");  
        ps.setString(1, s.getStuNum());  
        ps.setString(2, s.getName());  
        System.out.println(s.getName());  
        ps.setString(3, s.getSex());  
        ps.setString(4, s.getAddress());  
        System.out.println("1");  
        ps.executeUpdate();  
        System.out.println("2");  
        DBUtil.close(conn);  
    }  
  
    public void delete(long id) throws Exception {  
        Connection conn = DBUtil.getConnection();  
        PreparedStatement ps = conn  
                .prepareStatement("delete from t_student where id = ?");  
        ps.setLong(1, id);  
        ps.executeQuery();  
        DBUtil.close(conn);  
  
    }  
  
    public Student find(long id) throws Exception {  
        Connection conn = DBUtil.getConnection();  
        PreparedStatement ps = conn  
                .prepareStatement("select * t_student where id=?");  
        ps.setLong(1, id);  
        ResultSet rs = ps.executeQuery();  
        Student s = new Student();  
        while (rs.next()) {  
            s.setId(id);  
            s.setStuNum(rs.getString("stuNum"));  
            s.setName(rs.getString("name"));  
            s.setSex(rs.getString("sex"));  
            s.setAddress(rs.getString("address"));  
        }  
        DBUtil.close(conn);  
        return s;  
    }  
  
    public List<Student> list() throws Exception {  
        Connection conn = DBUtil.getConnection();  
        Statement stmt = conn.createStatement();  
        ResultSet rs = stmt.executeQuery("select * from t_student");  
        List<Student> students = new ArrayList<Student>();  
        while (rs.next()) {  
            Student s = new Student();  
            s.setId(rs.getLong("id"));  
            s.setStuNum(rs.getString("stuNum"));  
            s.setName(rs.getString("name"));  
            s.setSex(rs.getString("sex"));  
            s.setAddress(rs.getString("address"));  
            students.add(s);  
        }  
        DBUtil.close(conn);  
        return students;  
    }  
  
    public void update(Student s) throws Exception {  
        Connection conn = DBUtil.getConnection();  
        PreparedStatement ps = conn  
                .prepareStatement("update t_student(stuNum,name,sex,address) values(?,?,?,?)");  
        ps.setString(1, s.getStuNum());  
        ps.setString(2, s.getName());  
        ps.setString(3, s.getSex());  
        ps.setString(4, s.getAddress());  
        ps.executeUpdate();  
        DBUtil.close(conn);  
    }  
  
}  

 

posted @ 2012-11-30 10:36  if_only  阅读(244)  评论(0编辑  收藏  举报
回到顶部