DAO设计 2017.12.21

本文介绍了一个使用Java进行数据库操作的示例项目,包括数据库连接的建立、基本的CRUD操作实现等。通过具体代码展示了如何插入、更新、删除及查询数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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{

@Override
public 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;
}

@Override
public 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;
}

@Override
public int deleteDept(int did) {
    String sql ="delect from student where did=?";
    Object [] object = {did};
    int num = this.update(sql, object);
    return num;
}

@Override
public 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;
}

@Override
public 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;
}
@Override
public 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值