DAO设计 2017.12.21

来源:互联网 发布:淘宝抢优惠券软件 编辑:程序博客网 时间:2024/04/28 15:25

package com.bdqn.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.bdqn.utils.Contants;

public class BaseDao {
private static Connection connection = null;
//在项目启动加载
/*static{
init();
}*/

//初始化配置文件public static void init() {    try {        Properties properties = new Properties();        //得到文件路径        String fileName = "database.properties";        //文件加载到流里面        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(fileName);        //读取配置文件        properties.load(is);        Contants.driver = properties.getProperty("driver");        Contants.url = properties.getProperty("url");        Contants.user = properties.getProperty("user");        //Contants.pwd = properties.getProperty("password");    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }}//获得连接public static Connection getConnection () {    //加载驱动    try {        Class.forName(Contants.driver);    } catch (ClassNotFoundException e1) {        // TODO Auto-generated catch block        e1.printStackTrace();    }    try {        connection = DriverManager.getConnection(Contants.url, Contants.user, null);    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } return connection;}//关闭资源public static void close (Connection connection) {          if(connection !=null){    try {        connection.close();    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }}   

}
//增删改方法
public static int update (String sql,Object[]object){
int num = 1;
PreparedStatement ps = null ;
connection = getConnection();
try {
ps = connection.prepareStatement(sql);

        if(object != null && object.length >0) {            for (int i = 0; i < object.length; i++) {                ps.setObject((i+1), object[i]);            }                       }        num = ps.executeUpdate();    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    return num;         }     //查询public static ResultSet getResueltSet(String sql,Object[]object) {    PreparedStatement ps = null;    ResultSet rs = null;    try {        ps = connection.prepareStatement(sql);        connection = getConnection();        if(object != null && object.length>0){            for (int i = 0; i < object.length; i++) {                ps.setObject((i+1),object[i]);            }        }        ps.executeQuery();    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }       return rs;} 

}


package com.bdqn.dao;

import java.util.List;

import com.bdqn.entity.Dept;

public interface DeptDao {
//增加

int insertDept (Dept dept);//修改int updateDept (Dept dept); //删除    int deleteDept (int did);//全查询List<Dept> selectAllDept ();//根据id查询List<Dept> selectDept (int did);

}


package com.bdqn.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bdqn.dao.BaseDao;
import com.bdqn.dao.DeptDao;
import com.bdqn.entity.Dept;

public class DeptImpl extends BaseDao implements DeptDao{

@Overridepublic int insertDept(Dept dept) {    String sql ="insert into student values(?,?,?,?)";    Object [] object = {dept.getDid(),dept.getDname(),dept.getDage(),dept.getDmesc()};    int num = this.update(sql, object);     return num;}@Overridepublic int updateDept(Dept dept) {    String sql = "update student set dname=? where did=?";    Object [] object ={dept.getDname(),dept.getDid()};    int num = this.update(sql, object);    return num;}@Overridepublic int deleteDept(int did) {    String sql ="delect from student where did=?";    Object [] object = {did};    int num = this.update(sql, object);    return num;}@Overridepublic List<Dept> selectAllDept() {    String sql = "select * from student";    ResultSet rs = this.getResueltSet(sql, null);    List<Dept> list = new ArrayList<Dept>();    try {        while(rs.next()) {            Dept dept = new Dept();            int did = rs.getInt("did");            String dname = rs.getString("dname");            int dage = rs.getInt("dage");            String dmesc = rs.getString("dmesc");            dept.setDid(did);            dept.setDname(dname);            dept.setDage(dage);            dept.setDmesc(dmesc);            list.add(dept);        }    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    return list;}@Overridepublic List<Dept> selectDept(int did) {    String sql ="select * from where did=?";     List<Dept> list = null;    Object []object = {did};    try {        ResultSet rs = this.getResueltSet(sql, object);        list = new ArrayList<Dept>();        while(rs.next()) {            Dept dept = new Dept ();            int did1 = rs.getInt("did");            String dname = rs.getString("dname");            int dage = rs.getInt("dage");            String dmesc = rs.getString("dmesc");            dept.setDid(did1);            dept.setDname(dname);            dept.setDage(dage);            dept.setDmesc(dmesc);            list.add(dept);        }    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    return list;}

}


package com.bdqn.entity;

public class Dept {

private int did;private String dname;private int dage;private String dmesc;public int getDid() {    return did;}public void setDid(int did) {    this.did = did;}public String getDname() {    return dname;}public void setDname(String dname) {    this.dname = dname;}public int getDage() {    return dage;}public void setDage(int dage) {    this.dage = dage;}public String getDmesc() {    return dmesc;}public void setDmesc(String dmesc) {    this.dmesc = dmesc;}public Dept(){}public Dept(int did, String dname, int dage, String dmesc) {    this.did = did;    this.dname = dname;    this.dage = dage;    this.dmesc = dmesc;}@Overridepublic String toString() {    return "Dept [did=" + did + ", dname=" + dname + ", dage=" + dage            + ", dmesc=" + dmesc + "]";}   

}


package com.bdqn.text;

import java.util.List;

import com.bdqn.dao.DeptDao;
import com.bdqn.dao.impl.DeptImpl;
import com.bdqn.entity.Dept;

public class Text {
public static void main(String[] args) {
DeptDao dao = new DeptImpl();
//测试insert
/*Dept dept = new Dept(10,”miss10”,100,”数据库连接10”);
int num = dao.insertDept(dept);
System.out.println(num);*/

    //测试update    Dept dept = new Dept();    dept.setDmesc("miss10");    dept.setDid(10);    int num = dao.updateDept(dept);    System.out.println(num);    //全查    /*List<Dept> list = dao.selectAllDept();    for (Dept dept : list) {        System.out.println(dept.toString());    }*/    /*List<Dept> list = dao.selectDept(2);    for (Dept dept : list) {        System.out.println(dept.toString());    }*/}

}


package com.bdqn.utils;

public class Contants {

public static String driver="com.mysql.jdbc.Driver";public static String url="jdbc:mysql://localhost:3306/student";public static String user="root";//public static String pwd="root";

}


database.properties

driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/dept
user = root
password =root

原创粉丝点击