如何在Spring Boot应用程序中使用连接池简化jdbc连接

时间:2019-07-20 09:44:24

标签: spring-boot

嗨,我已经在我的动态Web应用程序之一中编写了以下代码,该应用程序连接到数据库并执行存储过程,返回结果集 以这种方式,如果结果超过一行并且状态为true {“ rs”:[{},{},{}],“ status”:“ true”}

如果状态不是true {“ rs”:[{}],“状态”:“ false”}

 public String call_auth_SP() throws SQLException {
    Connection conn = null;
    PreparedStatement callableStatement = null;

    JSONObject jmain = null;
    JSONArray jarray = null;

    try {
        jmain = new JSONObject();
        String auth_DBUSER_access_SQL = null;
        if (inputs != null) {
        auth_DBUSER_access_SQL = "{call " + spName + "(" + inputs + ")}";
        } else {
        auth_DBUSER_access_SQL = "{call " + spName + "}";
        }
        logger.info(auth_DBUSER_access_SQL);

        conn = ds.getConnLive();
        callableStatement = conn.prepareStatement(auth_DBUSER_access_SQL);
        boolean isResultSet = callableStatement.execute();
        int count = 0;
        while (true) {
        if (isResultSet) {

            ResultSet rs = callableStatement.getResultSet();
            ResultSetMetaData rsmd = rs.getMetaData();
            jarray = new JSONArray();

            while (rs.next()) {
            JSONObject jobj = new JSONObject();

            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                if (rsmd.getColumnName(i).equals("status")) {
                if (rs.getString(i).equals("TRUE")) {
                    jmain.put("status", "true");
                } else {
                    jmain.put("status", "false");
                }
                } else {
                String column_name = rsmd.getColumnLabel(i);
                // logger.info(column_name);
                jobj.put(column_name, rs.getObject(column_name));
                }
            }
            jarray.put(jobj);
            }
            rs.close();
        } else {
            if (callableStatement.getUpdateCount() == -1)
            break;
        }
        count++;
        isResultSet = callableStatement.getMoreResults();
        }
        jmain.put("rs", jarray);
        conn.close();
        conn = null;
        callableStatement.close();

    } catch (Exception e) {
        jarray = new JSONArray();
        jarray.put(new JSONObject(
            "{\"UserMsg\":\"We are experiencing issues, please try again\",\"ErrorMsg\":\"" + e.getMessage() + "\"}"));
        jmain.put("status", "false");
        jmain.put("rs", jarray);
        logger.error(e);
    } finally {
        if (callableStatement != null) {
        callableStatement.close();
        }
        if (conn != null) {
        conn.close();
        }
    }

    return jmain.toString();
    }

如何在Spring Boot应用程序中实现相同的目标

0 个答案:

没有答案