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,工厂
   提供符合接口定义的对象,调用者不用关心
   对象的创建细节。
   也就是说,通过工厂,可以将调用者与要调用的
   对象解耦了。

 

StudentDAO=dao.impl.StudentDAOJdbcImpl

 

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"));
	}
}

 

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;
	}
}

 

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;
}

 

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);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值