cn.itcast.bbs.dao.TopicDao.java Source code

Java tutorial

Introduction

Here is the source code for cn.itcast.bbs.dao.TopicDao.java

Source

/**
 * @(#)TopicDao.java, 20151011. 
 * 
 * Copyright 2015 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package cn.itcast.bbs.dao;

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.itcast.bbs.domain.Topic;
import cn.itcast.bbs.util.JdbcUtil;

/**
 *
 * @author Administrator
 *
 */
public class TopicDao {
    /*
     * ??
     */
    public List<Topic> showAllTopicByTypeId(int id) throws SQLException {
        List<Topic> topicList = null;
        QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "select *from topic where type_id = ? order by time desc;";
        topicList = runner.query(sql, new BeanListHandler(Topic.class), id);
        return topicList;
    }

    /*
     * ??
     */
    public int countTopicByType(int id) throws SQLException {
        QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "select count(*) from topic where type_id = ?;";
        Long cnt = runner.query(sql, new ScalarHandler(), id);
        return cnt.intValue();
    }

    /*
     * ?
     */
    public void addTopic(Topic topic, int id) throws SQLException {
        QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "insert into topic(title,name,content,type_id) values(?,?,?,?);";
        runner.update(sql, new Object[] { topic.getTitle(), topic.getName(), topic.getContent(), id });
    }

    /*
     * ?id?
     */
    public Topic findTopicById(int id) throws SQLException {
        QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "select *from topic where id = ?;";
        Topic topic = runner.query(sql, new BeanHandler(Topic.class), id);
        return topic;
    }

    /*
     * 
     */
    public void updateTopic(Topic topic) throws SQLException {
        QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
        String sql = "update topic set title=?,content=? where id=?;";
        runner.update(sql, new Object[] { topic.getTitle(), topic.getContent(), topic.getId() });
    }

    /*
     * ??id
     */
    public void deleteTopicByTypeId(int typeId) throws SQLException {
        QueryRunner runner = new QueryRunner();
        String sql = "delete from topic where type_id = ?;";
        runner.update(JdbcUtil.getConnection(), sql, typeId);
    }
}