com.ouc.cpss.dao.BaseDao.java Source code

Java tutorial

Introduction

Here is the source code for com.ouc.cpss.dao.BaseDao.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ouc.cpss.dao;

/**
 *
 * @author LIUYIYU
 */
import java.sql.*;
import java.util.List;

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

/**
 * ??
 */
public class BaseDao {

    /**
     * 
     *
     * @return
     */
    public Connection getConnection() {
        Connection conn = null;
        String jdbcURL = "jdbc:mysql://localhost:3306/cpss? useUnicode=true&characterEncoding=UTF8";
        String jdbcDriver = "com.mysql.jdbc.Driver";
        String user = "root";
        String password = "527611";
        try {
            //DbUtils
            DbUtils.loadDriver(jdbcDriver);
            conn = DriverManager.getConnection(jdbcURL, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * sql?,?,,
     *
     * @param sql
     * @return
     */
    public boolean update(String sql, Object[] params) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = getConnection();
            QueryRunner qRunner = new QueryRunner();
            int i = qRunner.update(conn, sql, params);
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return flag;
    }

    /**
     * 
     *
     * @param sql
     * @param clazz
     * @return
     */
    public List query(String sql, Class clazz) {
        List beans = null;
        Connection conn = null;
        try {
            conn = getConnection();
            QueryRunner qRunner = new QueryRunner();
            beans = (List) qRunner.query(conn, sql, new BeanListHandler(clazz));
            //BeanListHandler?ResultSet???List?
            //????ResultSet
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return beans;
    }

    /**
     * ?
     *
     * @param sql
     * @param clazz
     * @return
     */
    public List query(String sql, Class clazz, Object[] params) {
        List beans = null;
        Connection conn = null;
        try {
            conn = getConnection();
            QueryRunner qRunner = new QueryRunner();
            beans = (List) qRunner.query(conn, sql, new BeanListHandler(clazz), params);

            //BeanListHandler?ResultSet???List?
            //????ResultSet
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return beans;
    }

    /**
     * 
     *
     * @param sql
     * @param clazz
     * @return
     */
    public Object get(String sql, Class clazz) {
        Object obj = null;
        Connection conn = null;
        try {
            conn = getConnection();
            QueryRunner qRunner = new QueryRunner();
            obj = qRunner.query(conn, sql, new BeanHandler(clazz));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return obj;
    }

    /**
     * ??
     *
     * @param sql
     * @param clazz
     * @return
     */
    public Object get(String sql, Class clazz, Object[] params) {
        Object obj = null;
        Connection conn = null;
        try {
            conn = getConnection();
            QueryRunner qRunner = new QueryRunner();
            obj = qRunner.query(conn, sql, new BeanHandler(clazz), params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(conn);
        }
        return obj;
    }

    /**
     * ?
     *
     * @param conn 
     * @param sql sql?
     * @param params ??
     * @return
     */
    public boolean batchUpdate(Connection conn, String sql, Object[][] params) throws SQLException {
        QueryRunner qRunner = new QueryRunner();
        int result = 0;
        boolean flag = false;
        result = qRunner.batch(conn, sql, params).length;
        if (result > 0) {
            flag = true;
        }
        return flag;
    }

    /**
     * ??
     *
     * @param conn 
     * @param sql sql?
     * @param params ?
     * @return
     * @throws java.sql.SQLException
     */
    public boolean update(Connection conn, String sql, Object[] params) throws SQLException {
        boolean flag = false;
        QueryRunner qRunner = new QueryRunner();
        int i = qRunner.update(conn, sql, params);
        if (i > 0) {
            flag = true;
        }
        return flag;
    }

}