宠物类Dao小练习

vo层

package com.neusoft.vo;

public abstract class Pet {
    private int id;
    private String name;
    private int health;
    private int love;
    public Pet() {
        super();
    }
    public Pet(int id, String name, int health, int love) {
        super();
        this.id = id;
        this.name = name;
        this.health = health;
        this.love = love;
    }
    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 int getHealth() {
        return health;
    }
    public void setHealth(int health) {
        this.health = health;
    }
    public int getLove() {
        return love;
    }
    public void setLove(int love) {
        this.love = love;
    }


}
package com.neusoft.vo;

public class Dog extends Pet{
    private String strain;

    public Dog() {
        super();
    }

    public Dog(String strain) {
        super();
        this.strain = strain;
    }

    public Dog(int id,String name,int health,int love,String strain){
        super.setId(id);
        super.setName(name);
        super.setHealth(health);
        super.setLove(love);
        this.strain = strain;
    }

    public String getStrain() {
        return strain;
    }

    public void setStrain(String strain) {
        this.strain = strain;
    }


}

Dao层
BaseDao

package com.neusoft.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
    // 提供数据库连接的四要素:驱动(Driver) 数据库的地址:(URL) 登录名(uname) 密码:(pwd)
    private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    private static final String UNAME = "system";
    private static final String UPWD = "123456";
    // 使用静态块来加载数据库驱动
    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载异常:" + e.getMessage());
        }
    }

    private Connection conn = null;// 声明数据库连接对象
    protected ResultSet res = null;// 声明数据库操作返回结果集对象
    private PreparedStatement pstmt = null;// 声明数据库操作预处理对象

    /**
     * 创建数据库连接对象
     */
    public void getConn() {
        try {
            conn = DriverManager.getConnection(URL, UNAME, UPWD);
        } catch (SQLException e) {
            System.out.println("数据库连接失败:"+e.getMessage());
        }
    }

    /**
     * 释放资源关闭数据库连接
     */
    protected void closeAll() {
        if (null != res) {
            try {
                res.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (null != pstmt) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (null != conn) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 执行数据库的查询功能
     * 
     * @param sql
     * @param params
     * @return
     */
    public ResultSet excuteQuery(String sql, Object[] params) {
        // 连接数据库
        this.getConn();
        try {
            // 创建预处理对象
            pstmt = conn.prepareStatement(sql);
            // 预处理条件参数
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i+1, params[i]);
                }
            }
            // 执行数据库的操作返回结果集
            res = pstmt.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 执行数据库的增,删,改操作
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String sql,Object[] params){
        int count = 0;
        this.getConn();
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i+1, params[i]);
            }
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            this.closeAll();
        }
        return count;
    }

}

IDogDao接口

package com.neusoft.dao.dog;

import java.util.List;

import com.neusoft.vo.Dog;
/**
 * 宠物_狗的操作功能定义
 * @author xxf
 *
 */
public interface IDogDao {
    /**
     * 根据编号查询狗狗
     * @param id
     * @return
     */
    public Dog searchById(int id);
    /**
     * 查询狗狗信息列表
     * @return
     */
    public List<Dog> searchDogList();
    /**
     * 添加狗狗
     * @param dog
     * @return
     */
    public int addDog(Dog dog);
    /**
     * 删除狗信息
     * @param id
     * @return
     */
    public int delDog(int id);
    /**
     * 修改狗信息
     * @param dog
     * @return
     */
    public int updateDog(Dog dog);
}

IDogDaoImpl实现类

package com.neusoft.dao.dog.impl;

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

import com.neusoft.dao.BaseDao;
import com.neusoft.dao.dog.IDogDao;
import com.neusoft.vo.Dog;

public class DogDaoImpl extends BaseDao implements IDogDao {

    @Override
    public int addDog(Dog dog) {
        // 编写SQL命令
        String sql = "INSERT INTO dog values(seq_dogid.nextval,?,?,?,?)";
        // 创建条件参数数组
        Object[] params = { dog.getName(), dog.getHealth(), dog.getLove(),
                dog.getStrain() };
        // 调用父类的操作功能操作数据库
        int count = super.executeUpdate(sql, params);
        return count;
    }

    @Override
    public int delDog(int id) {
        String sql = "DELETE FROM dog WHERE dogId=?";
        Object[] params = { id };
        int count = super.executeUpdate(sql, params);
        return count;
    }

    @Override
    public Dog searchById(int id) {
        Dog dog = null;
        String sql = "SELECT dogid,dogname, doghealth,doglove,dogstrain FROM dog WHERE dogId=?";
        Object[] params = { id };
        res = super.excuteQuery(sql, params);
        try {
            if (res.next()) {
                dog = new Dog();
                dog.setHealth(res.getInt("doghealth"));
                dog.setId(res.getInt("dogid"));
                dog.setLove(res.getInt("doglove"));
                dog.setName(res.getString("dogname"));
                dog.setStrain(res.getString("dogstrain"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            this.closeAll();
        }
        return dog;
    }

    @Override
    public List<Dog> searchDogList() {
        List<Dog> dogList = new ArrayList<Dog>();
        Dog dog = null;
        String sql = "SELECT dogid,dogname, doghealth,doglove,dogstrain FROM dog";
        Object[] params = null;
        res = super.excuteQuery(sql, params);
        try {
            while (res.next()) {
                dog = new Dog();
                dog.setHealth(res.getInt("doghealth"));
                dog.setId(res.getInt("dogid"));
                dog.setLove(res.getInt("doglove"));
                dog.setName(res.getString("dogname"));
                dog.setStrain(res.getString("dogstrain"));
                dogList.add(dog);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            this.closeAll();
        }
        return dogList;
    }

    @Override
    public int updateDog(Dog dog) {
        // TODO Auto-generated method stub
        String sql = "update dog set dogname=?,doghealth=?,doglove=?,dogstrain=? where dogid = ?";
        Object[] params = {dog.getName(),dog.getHealth(),dog.getLove(),dog.getStrain()};
        int count = super.executeUpdate(sql, params);
        return count;
    }

}

测试类
Test

package com.neusoft.test;

import java.util.Iterator;
import java.util.List;
import com.neusoft.dao.dog.IDogDao;
import com.neusoft.dao.dog.impl.DogDaoImpl;
import com.neusoft.vo.Dog;

public class Test {
    public static void main(String[] args) {
        System.out.println("显示狗类列表");
        IDogDao d = new DogDaoImpl();
        List<Dog> doglist = d.searchDogList();
        Iterator<Dog> it = doglist.iterator();
        while (it.hasNext()) {
            Dog dog = it.next();
            System.out.println(dog.getId() + "\t" + dog.getName() + "\t"
                    + dog.getHealth() + "\t" + dog.getLove());
        }
    }

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014535776/article/details/52347550
文章标签: Dao
想对作者说点什么? 我来说一句

java学习练习题及答案

练习

high2011 high2011

2015-08-09 17:55:31

阅读数:1632

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭