数据库操作工具类

来源:互联网 发布:sai mac版本 编辑:程序博客网 时间:2024/05/21 09:29

DatabaseUtil.java

package com.lmb.common.util;/** * 数据库对象操作处理模块: *      用于数据库对象处理 */import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import com.lmb.exception.CustomizeException;public class DatabaseUtil {    private static final Log logger = LogFactory.getLog(DatabaseUtil.class);    private static DatabaseUtil manager = null;    public static synchronized DatabaseUtil newInstance() {        if (manager == null) {            manager = new DatabaseUtil();        }        return manager;    }    /**     * 取得JNDI数据源     *      * @param dsname     * @return     * @throws CustomizeException     */    public DataSource getJndiDatasource(String dsname) throws CustomizeException {        DataSource dataSource = null;        try {            InitialContext ctx = new InitialContext();            dataSource = (DataSource) ctx.lookup(dsname);        } catch (NamingException e) {            throw new CustomizeException(e);        }        return dataSource;    }    /**     * 从连接池中取得数据库连接     *      * @param dsname     * @return     * @throws CustomizeException     */    public Connection getJndiConnection(String dsname) throws CustomizeException {        Connection conn = null;        try {            DataSource ds = getJndiDatasource(dsname);            conn = ds.getConnection();        } catch (SQLException e) {            throw new CustomizeException(e);        }        return conn;    }    /**     * 使用JDBC取得数据库连接     *      * @param drivername     * @param url     * @param username     * @param password     * @return     * @throws CustomizeException     */    public Connection getJdbcConnection(String drivername, String url,            String username, String password) throws CustomizeException {        Connection conn = null;        try {            Class.forName(drivername).newInstance();            conn = DriverManager.getConnection(url, username, password);        } catch (Exception e) {            throw new CustomizeException(e);        }        return conn;    }    /**     * 关闭数据库连接     *      * @param conn     */    public void closeConnection(Connection conn) {        if (conn != null) {            try {                conn.close();            } catch (SQLException e) {                logger.error(e);            }        }    }    /**     * 关闭参数查询语句     *      * @param pstmt     */    public void closePreparedStatement(PreparedStatement pstmt) {        if (pstmt != null) {            try {                pstmt.close();            } catch (SQLException e) {                logger.error(e);            }        }    }    /**     * 关闭存储过程查询语句     *      * @param pstmt     */    public void closeCallableStatement(CallableStatement stmt) {        if (stmt != null) {            try {                stmt.close();            } catch (SQLException e) {                logger.error(e);            }        }    }    /**     * 关闭普通查询语句     *      * @param pstmt     */    public void closeStatement(Statement stmt) {        if (stmt != null) {            try {                stmt.close();            } catch (SQLException e) {                logger.error(e);            }        }    }    /**     * 关闭结果集     *      * @param rs     */    public void closeResultSet(ResultSet rs) {        if (rs != null) {            try {                rs.close();            } catch (SQLException e) {                logger.error(e);            }        }    }}
1 0
原创粉丝点击