缩短代码JDBC

时间:2018-05-24 20:28:26

标签: java mysql jdbc

我需要帮助缩短此代码。我的项目包含此部分32x。所以我想问一下是否有人不知道如何简化和概括它。

conn[1] = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

    {
        PreparedStatement stmt = conn[1].prepareStatement("SELECT * FROM nfl.minnesota");
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            teamA = rs.getInt("MinnvsAt");
            teamB = rs.getInt("AtvsMinn");
            ID = rs.getBoolean("MinnvsAtP");
            placement();
            place();
        }
        PreparedStatement stmt1 = conn[1].prepareStatement("SELECT SUM(MinnvsAt) FROM nfl.minnesota WHERE MinnvsAtP = TRUE");
        ResultSet rs1 = stmt1.executeQuery();
        rs1.next();
        int SUMPointsAH = rs1.getInt(1);

        PreparedStatement stmt2 = conn[1].prepareStatement("SELECT SUM(MinnvsAt) FROM nfl.minnesota WHERE MinnvsAtP = FALSE");
        ResultSet rs2 = stmt2.executeQuery();
        rs2.next();
        int SUMPointsAA = rs2.getInt(1);

        PreparedStatement stmt3 = conn[1].prepareStatement("SELECT SUM(AtvsMinn) FROM nfl.minnesota WHERE MinnvsAtP = TRUE");
        ResultSet rs3 = stmt3.executeQuery();
        rs3.next();
        int SUMPointsBH = rs3.getInt(1);

        PreparedStatement stmt4 = conn[1].prepareStatement("SELECT SUM(AtvsMinn) FROM nfl.minnesota WHERE MinnvsAtP = FALSE");
        ResultSet rs4 = stmt4.executeQuery();
        rs4.next();
        int SUMPointsBA = rs4.getInt(1);
        pointsH.add(SUMPointsAA);

    }

2 个答案:

答案 0 :(得分:2)

您可以使用一些参数进行4次查询,2次查询,并提取两个辅助方法,使其更加干燥,但总体而言是为了使您的代码更加健壮和可读,因为您声明了太多的变量,而且范围太宽。
你可以这样写:

public int executeMinnvsAtPQuery(String MinnvsAtP){
        String query = "SELECT SUM(MinnvsAt) FROM nfl.minnesota WHERE MinnvsAtP = ?";
        PreparedStatement stmt = conn[1].prepareStatement(minnvsAtPQuery);
        stmt.setString(1, MinnvsAtP);
        ResultSet rs = stmt.executeQuery();
        rs.next();
        return rs.getInt(1);
}

public int executeAtvsMinnQuery(String AtvsMinn){
        String query = "SELECT SUM(AtvsMinn) FROM nfl.minnesota WHERE MinnvsAtP = ?";
        PreparedStatement stmt = conn[1].prepareStatement(minnvsAtPQuery);
        stmt.setString(1, AtvsMinn);
        ResultSet rs = stmt.executeQuery();
        rs.next();
        return rs.getInt(1);
}

以这种方式使用它们:

int SUMPointsAH = executeMinnvsAtPQuery("TRUE");
int SUMPointsAA =  executeMinnvsAtPQuery("FALSE");

int SUMPointsBH  = executeAtvsMinnQuery("TRUE");
int SUMPointsBA =  executeAtvsMinnQuery("FALSE");

请注意,即使两个查询非常接近,我也不认为您可以通过sum(...)使PreparedStatement汇总参数有价值。
我不建议连接既容易出错又不安全的SQL部分。

  

我的项目包含此部分32x。

要将此值减少到1X,您可以遵循相同的逻辑并在一个方法中提取这些查询集,其中包含您可以在任何地方调用的一些参数。

另请注意,查询执行可能会在执行时间方面产生成本。因此,您还应确保优化和要求您的查询。

答案 1 :(得分:0)

  1. 您可以通过同时检索两列来减少执行的查询的实际数量。
  2. 您可以通过参数提供TRUE或FALSE。

    因此,您需要的唯一PreparedStatementSELECT SUM(MinnvsAt), SUM(AtvsMinn) FROM nfl.minnesota WHERE MinnvsAtP = ?

    所以你可以将整个事情减少一半以上。

  3. 如@GriffeyDog所述,您可以通过GROUP BY在一个查询中完成所有操作:

    SELECT SUM(MinnvsAt), SUM(AtvsMinn) FROM nfl.minnesota GROUP BY MinnvsAtP  ORDER BY MinnvsAtP
    

    因此,第一行将是FALSE结果,第二行将是TRUE结果,每行的第一行将是SUM(MinnvsAt),第二行将是SUM(AtvsMinn)

    因此只需要执行一个PreparedStatement.executeQuery()