基于Array列表创建SQL

时间:2013-04-16 05:45:10

标签: java mysql sql jdbc

AIM:我正在尝试创建和运行一个sql查询,其数量或时间与我在Array List中的值相同,并将它们纳入SQL

  

数组列表://这将获取所有团队成员的名称并将其添加到   数组列表

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数组中的值,并作为该循环的一部分插入第二个sql中的al数组的值,然后将这些查询的结果添加到另一个数组al1。

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

感谢任何帮助建议。

干杯。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

第一个建议:使用foreign key了解关于关系数据库和表关系的更多信息,您将节省大量时间来加入这些表而不是name列。

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

第三个建议:从不String级联传递参数,而是使用PreparedStatement来阻止SQL Injection次攻击。

第四个建议:如果您检索List<SomeClass>而不是普通ArrayList<String>会更好。这是因为您programming to interfaces, not to implementationSomeClass可以保存您需要检索的属性。

将所有这些概念联系起来,你会得到类似的结论:

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) {}
}