CSDNRGY 2017-12-01 09:24 采纳率: 88.2%
浏览 960
已采纳

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

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;
    }

}
  • 写回答

1条回答 默认 最新

  • springcloud-zeng 2017-12-01 09:25
    关注

    用junit写一个测试类 crud都测试一遍。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!