[Java] 数据库连接管理类

package com.wdcloud.monitoring.common;
    /**
     * @Description: TODO
     * @date: 2015��11��19�� ����10:23:16
     * @version: 1.0
     * @update [����YYYY-MM-DD] [���������][�������]
     */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mysql.jdbc.Statement;

/**
 * 连接数据库的工具类,被定义成不可继承且是私有访问
 */
public final class DBUtils {
    private static String url = "jdbc:mysql://localhost:3306/test";
    private static String user = "root";
    private static String psw = "root";
    
    private static  Connection conn;
    private static Statement statement;
    
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    private DBUtils() {
        
    }
    
    /**
     * 获取数据库的连接
     * @return conn
     */
    public static Connection getConnection() {
        if(null == conn) {
            try {
                conn = DriverManager.getConnection(url, user, psw);
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return conn;
    }
    
    public static Statement getStatement(){
        if(null == statement){
            try{
                statement = (Statement) DBUtils.getConnection().createStatement();
            }catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
        return statement;
    }
    
    /**
     * 释放资源
     * @param conn
     * @param pstmt
     * @param rs
     */
    public static void closeResources(Connection conn,PreparedStatement pstmt,ResultSet rs) {
        if(null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } finally {
                if(null != pstmt) {
                    try {
                        pstmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    } finally {
                        if(null != conn) {
                            try {
                                conn.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }
            }
        }
    }
    
    /**
     * 
         * @Description: TODO 将ResultSet转成list
         * @date: 2015年11月19日 下午2:08:25
         * @version: 1.0
         * @param rs
         * @return
         * @throws java.sql.SQLException    
         * @List 
         * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public static List resultSetToList(ResultSet rs) throws java.sql.SQLException {   
        if (rs == null)   
            return Collections.EMPTY_LIST;   
        ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等   
        int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数   
        List list = new ArrayList();   
        Map rowData = new HashMap();   
        while (rs.next()) {   
         rowData = new HashMap(columnCount);   
         for (int i = 1; i <= columnCount; i++) {   
                 rowData.put(md.getColumnName(i), rs.getObject(i));   
         }   
         list.add(rowData);   
        }   
        return list;   
}
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值