首页 新闻 会员 周边 捐助

如何判断我自己封装的jdbc有没有bug?

0
[已解决问题] 解决于 2017-12-06 09:12
package util;

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.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

public class Jdbc {

    private static ResourceBundle res = ResourceBundle.getBundle("jdbc");
    private static String url = res.getString("db.url");
    private static String user = res.getString("db.user");
    private static String psw = res.getString("db.password");
    private static String driverClass = res.getString("driverClass");
    private static Integer poolSize = Integer.parseInt(res.getString("poolSize"));
    private static LinkedList<Connection> connections = new LinkedList<Connection>();  
    
    private Connection conn;
    private ResultSet rs;
    private ResultSetMetaData md;
    private PreparedStatement ps;
    private List<Map<String,Object>> list;
    private Map<String,Object> map;
    
    static{
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        initConnPool();
    }
    //初始化连接池
    private static void initConnPool() {
        for (int i = 0; i < poolSize ; i++) {
            try {
                Connection conn = DriverManager.getConnection(url, user, psw);
                connections.add(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }    
    }
    
    //拿出一个连接
    private synchronized void getConnection() {
        conn = connections.removeFirst();
    }
    
    //放回一个连接
    private synchronized void backConnection(Connection connection,PreparedStatement ps,ResultSet rs) {
        try {
            if(ps!=null) ps.close();
            if(rs!=null) rs.close();
            connections.addLast(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
    }

    //执行数据查询语句
    public List<Map<String,Object>> dql(String sql,Object...objs){
        getConnection();
        list = new ArrayList<Map<String,Object>>();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 1; i <= objs.length; i++) {
                ps.setObject(i, objs[i]);
            }
            rs = ps.executeQuery();
            md = rs.getMetaData();
            while (rs.next()) {
                map = new HashMap<String,Object>();
                for (int i = 1; i <= md.getColumnCount(); i++) {
                    String key = md.getColumnName(i);
                    Object value = rs.getObject(i);
                    map.put(key, value);
                }
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            backConnection(conn,ps,rs);
        }
        return list;
    }
    
    //执行数据管理语句
    public boolean dml(String sql,Object...objs){
        getConnection();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 1; i <= objs.length; i++) {
                ps.setObject(i, objs[i]);
            }
            return ps.executeUpdate()==1?true:false;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            backConnection(conn,ps,rs);
        }
        return false;
    }
    
}
renguanyu的主页 renguanyu | 小虾三级 | 园豆:841
提问于:2017-12-01 16:03
< >
分享
最佳答案
0

拿自己封装的在不同场景使用一下就知道啦,或者直接找常用的jdbc第三方代码比对一下,大差不差就好

奖励园豆:5
大宇007 | 菜鸟二级 |园豆:331 | 2017-12-04 18:05
其他回答(1)
0

连接池这块最好还是用第三方的,你这块只是一个list来存放有很多场景没办法处理的,比如idle过长的连接的回收,断开重连和链接可用性的校验等。

Daniel Cai | 园豆:10424 (专家六级) | 2017-12-01 17:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册