jdbc连接数据库,查询语句返回对象

大概代码贴一下,自己连接,仅执行语句返回内容

/**
     * 查询sql
     *
     * @param: sql
     * @return: JSONArray
     */
    public static JSONArray querySql(PostgreSQLConfig staticPostgreSQLConfig, String sql, Object... objects) {
        JSONArray resJson = null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet reSet = null;
        PGConnectionPool connPool = null;
        try {
//            conn = getConnection(staticPostgreSQLConfig);
            connPool = PGConnectionPoolUtil.getPoolInstance(staticPostgreSQLConfig);
            conn = connPool.getConnection();
            pstmt = conn.prepareStatement(sql);
            if (objects != null && objects.length > 0) {
                for (int i = 0; i < objects.length; i++) {
                    pstmt.setObject(i + 1, objects[i]);
                }
            }
            logger.info("执行SQL:{}", sql);
            reSet = pstmt.executeQuery();
            // 获取列数
            ResultSetMetaData metaData = reSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            // 遍历ResultSet中的每条数据
            while (reSet.next()) {
                JSONObject jsonObj = new JSONObject();
                // 获取每一列数据
                for (int i = 1; i <= columnCount; i++) {
                    String value = "";
                    String columnName = metaData.getColumnLabel(i);//列名称
                    if (reSet.getString(columnName) != null && !reSet.getString(columnName).equals("")) {
                        value = new String(reSet.getBytes(columnName), "UTF-8");//列的值,有数据则转码
                    }
                    jsonObj.put(columnName, value);
                }
                resJson.add(jsonObj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            closeConnection(conn, pstmt, reSet);
            closePoolConnection(connPool, conn, pstmt, reSet);
        }
        return resJson;
    }
 
    public static <T> List<T> querySql(PostgreSQLConfig staticPostgreSQLConfig, Class<T> clazz, String sql, Object... objects) {
        List<T> resList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet reSet = null;
        PGConnectionPool connPool = null;
        try {
//            conn = getConnection(staticPostgreSQLConfig);
            connPool = PGConnectionPoolUtil.getPoolInstance(staticPostgreSQLConfig);
            conn = connPool.getConnection();
            pstmt = conn.prepareStatement(sql);
            if (objects != null && objects.length > 0) {
                for (int i = 0; i < objects.length; i++) {
                    pstmt.setObject(i + 1, objects[i]);
                }
            }
            logger.info("执行SQL:{}", sql);
            reSet = pstmt.executeQuery();
            List<Map<String, Object>> tempList = new ArrayList<>();
            Map<String, Object> tempMap = null;
            // 获取列数
            ResultSetMetaData metaData = reSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            // 遍历ResultSet中的每条数据
            while (reSet.next()) {
                tempMap = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnLabel(i);//列名称
                    tempMap.put(columnName, reSet.getObject(i));
                }
                tempList.add(tempMap);
            }
            //加载数据到对象
            if (tempList.size() > 0) {
                for (Map<String, Object> map : tempList) {
                    //解析对象
                    T bean = clazz.newInstance();
                    for (Map.Entry<String, Object> entry : map.entrySet()) {
                        String propertyName = entry.getKey();
                        Object value = entry.getValue();
                        Field field = clazz.getDeclaredField(propertyName);
                        String str = field.getName();
                        String s = str.substring(0, 1).toUpperCase() + str.substring(1);
                        Method method = clazz.getMethod("set" + s, field.getType());
                        method.invoke(bean, value);
                    }
                    resList.add(bean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            closeConnection(conn, pstmt, reSet);
            closePoolConnection(connPool, conn, pstmt, reSet);
        }
        return resList;
    }
/**
     * 关闭连接
     */
    public static void closePoolConnection(PGConnectionPool connPool, Connection conn, PreparedStatement pstmt, ResultSet reSet) {
        try {
            if (pstmt != null) {
                pstmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (reSet != null) {
                try {
                    reSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (conn != null) {
                            connPool.returnConnection(conn);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  

  

posted @ 2022-04-08 08:56  小小菜包子  阅读(158)  评论(1)    收藏  举报