宠物类Dao小练习

原创 2016年08月28日 20:23:53

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

}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

宠物类Dao小练习

vo层package com.neusoft.vo;public abstract class Pet { private int id; private String name; ...

养六角恐龙的误区

误区1:用养鱼的方法养六角恐龙---养水、各种超级过滤、底砂、硝化细菌…… 六角恐龙浑身上下没有一点鳞片,任何动物都是--身体越光滑、越软对疾病抵抗力越差。 养好的水是老水,时间太久,硬度相对...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

宠物城堡抢先评测:LBS宠物小精灵

金山西山居工作室进军移动游戏的第一款产品终于揭开了它的神秘面纱,这款日系动漫风格的LBS+角色扮演+模拟经营的大杂烩移动游戏将在5月正式登陆Android平台,而小编已经拿到内测资料找让大家先睹为快一...

宠物美容师行业有哪些优势

21实际想要自己在社会上立足,要么有脑子,要么有技术。灵活的头脑不是每一个人都具备的,所以我们必须要掌握一门过硬的技术才能在社会上立足。针对宠物美容行业今天我们重点来介绍一下宠物美容师行业有哪些优势?...

java--宠物店

//宠物接口——————Pet.java interface Pet{  public String getName();  public String getColor();  public...

电子宠物

import java.util.*; class Dog { private String name="hua";   private int health=100;   private i...

ZZY的宠物

ZZY的宠物 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 99  Solved: 51 [Submit][Status][Web Bo...

关于宠物

记得以前面试的时候,曾被问过一个问题,如果让你设计一只宠物,该如何实现?当时还只是小菜鸟一只(嘿嘿,其实现在仍然是菜鸟),我回答的是让npc静止,玩家移动的同时把宠物强行拉到自己附近。(如果现在问,我...

虚基类练习:动物

Problem D: 虚基类练习:动物 [Submit][Status][Web Board] Description 长期的物种进化使两栖动物既能活跃在陆地上,又能游动于水中。利用虚基类建立...

虚基类练习-动物

/* *程序的版权和版本声明部分: *Copyright(c)2014,烟台大学计算机学院学生 *All rights reserved. *文件名称: *作者:田成琳 *完成日期:201...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)