0

目标:我正在尝试创建和运行与我的数组列表中的值相同的数量或时间的 sql 查询,并将它们整合到 SQL 中

Array List: //这将获取所有团队成员的姓名并将他们添加到数组列表中

ResultSet rs = name.executeQuery("SELECT name from TEAM_MEMBERS");
            while (rs.next()) 
              { 
                 al.add(rs.getString("name"));
              } 
            rs.close();

为团队的每个成员运行的 SQL。请注意,团队成员会发生变化,因此这不能是静态的。

ResultSet rs1 = name.executeQuery("SELECT sum(hours) FROM PROJECT_TIME WHERE DATE = '"+date+"' AND name = "+al+"");
            while (rs1.next()) 
              { 
                 al1.add(rs1.getString(1));
              } 
            rs1.close();

理想情况下,我想循环遍历 al 数组中的值,并作为该循环的一部分将 al 数组中的值插入到第二个 sql 中,然后将这些查询的结果添加到另一个数组 al1 中。

我认为一种解决方案是将 al 数组计数添加到 int 中,然后使用它来生成我的循环,然后我将通过带有基本增量 ++ 的 X 值将值插入循环中。然而,这似乎有点混乱,我认为有更好的解决方案,我不知道。

任何帮助建议表示赞赏。

干杯。

4

2 回答 2

0

第一个建议:阅读更多关于关系数据库和使用外键的表关系的信息,您将节省大量时间来连接这些表而不是name列。

第二个建议:您可以使用单个查询来使用JOIN句子获取所需的数据。

第三个建议:不要通过String连接传递参数,而是使用 aPreparedStatement来防止SQL 注入攻击。

第四个建议:如果您使用retrieveList<SomeClass>而不是plain会更好ArrayList<String>。这是因为您正在对接口进行编程,而不是对实现进行编程,并且您SomeClass可以保存需要检索的属性。

将所有这些概念联系起来,您将拥有如下内容:

MySQL 语法:

CREATE TABLE TEAM_MEMBERS
(id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL);

CREATE TABLE PROJECT_TIME
(id INT PRIMARY KEY AUTO_INCREMENT,
hours INT NOT NULL,
date DATE NOT NULL,
teamMemberId INT NOT NULL,
FOREIGN KEY (teamMemberId) REFERENCES TEAM_MEMBERS(id));

使用 JOIN 的基本查询

SELECT tm.name, sum(pt.hours) AS hours -- retrieving both name and sum of hours at the same time
FROM
    TEAM_MEMBERS tm --team members table
    INNER JOIN PROJECT_TIME pt ON pt.teamMemberId = tm.id -- joining team members with project time table (not sure if this really makes sense, but is based on your code example)
WHERE
    DATE = <some_date> -- used to show where the parameter will go
GROUP BY
    tm.name -- used when using SUM or another AGGREGATE functions

在 Java 端使用这个基本查询:

//sample about the class
public class TeamMember {
    private String name;
    private int projectHours;
    //constructor...
    public TeamMember() {
    }
    //constructor sending parameters (because I'm lazy when having a class with few parameters)
    public TeamMember(String name, int projectHours) {
        this.name = name;
        this.projectHours = projectHours;
    }
    //getter and setters...
}

List<TeamMember> teamMembers = new ArrayList<TeamMember>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
    con = ... //initialize the connection
    String query = "SELECT tm.name, sum(pt.hours) AS hours FROM TEAM_MEMBERS tm " +
        "INNER JOIN PROJECT_TIME pt ON pt.teamMemberId = tm.id " +
        "WHERE DATE = ? GROUP BY tm.name";
    pstmt = con.prepareStatement(query);
    pstmt.setString(1, date); //assuming you have your date in String format
    //pstmt.setDate(1, date); //you could use this if your date variable is java.util.Date
    rs = pstmt.execute();
    while(rs.next()) {
        teamMembers.add(new TeamMember(rs.getString("name"), rs.getInt("hours"));
    }
} catch (Exception e) {
    //handle the Exception
} finally {
    //close the resources (this is a MUST DO)...
    try {
        if (rs != null) {
            rs.close();
        }
    } catch (SQLException sqle) {}
    try {
        if (pstmt != null) {
            pstmt.close();
        }
    } catch (SQLException sqle) {}
    try {
        if (con != null) {
            con.close();
        }
    } catch (SQLException sqle) {}
}
于 2013-04-16T06:10:23.527 回答
0

为什么你不使用视图你可以从两个表中创建视图,并且只执行一个查询

于 2013-04-16T05:48:13.557 回答