0

所以我有这个sql查询:

    "select company, surveys.surveyID, questionID, question, questionScore FROM surveys INNER JOIN (SELECT * FROM companies INNER JOIN "
            + "(SELECT * FROM questions INNER JOIN categories "
            + "ON questions.catID = categories.catID) AS cats "
            + "ON companies.questionID = cats.questionID) AS yes "
            + "ON yes.surveyID = surveys.surveyID WHERE company=?"

“猫”和“是”没有任何意义,只是我极其冗长的命名方案的牺牲品。

公司只是一个字符串。

它返回的表如下所示:

 ---------------------------------------------
|companyName|surveyID|questionID|questionScore|
 ---------------------------------------------

此表的主键是 (companyName,surveyID AND QuestionID),因为每个公司可以有多个调查,每个调查都有许多问题。

然后我有这个类:

public class Company
{
private String companyName;
private String surveyorName;
private String surveyorTitle;
private int surveyID;
private Map<Integer,Integer> questions;

public Company(String name, String surveyor, String surveyerTitle,
    int surveyID, Map<Integer, Integer> questions)
{
this.companyName = name;
this.surveyorName = surveyor;
this.surveyorTitle = surveyerTitle;
this.surveyID = surveyID;
this.questions = questions;
}

拥有所有的 getter 和 setter

例如,假设有一家公司 Mens Insamen。SurveyID 是 1。所以

Mens Insamen    1       q1     3
Mens Insamen    1       q2     1

ETC...

现在我需要使用来自 sql 查询的数据填充公司对象列表。我已经尝试了一段时间,但无法真正想出任何东西。

如果(不出所料)不清楚(fokol(no)咖啡),我可以在某个时候改进它。

提前致谢

4

4 回答 4

0

您可以简单地使用 arraylist 和您的 while 循环来实现这一点
为循环外的 compines 创建您的 arraylist 并使用循环
将每个 Company 对象添加到此数组列表中

ArrayList<Company> compList = new ArrayList<Company>();

while{

/*read  values to variables and create an object of Company 
  then add to arraylist as follows **/

Company comp = new Company (name, surveyor, surveyerTitle,surveyID,questions);    
compList.add(comp);
}
于 2012-08-21T10:22:32.263 回答
0

创建类 CompanyMapper 在其中有一个方法,将该方法传递给 SQL ResultSet 作为输入,循环通过它并创建公司列表并返回它。

class CompanyMapper{

public List<Company> getCompanies(ResultSet rs){

    ArrayList<Company> compList = new ArrayList<Company>();
    while(...rs has entry...){
        //Add new Company to the list
    }
return compList;
}
于 2012-08-21T10:25:43.403 回答
0

真的很难说,到目前为止你有什么问题,但如果你想填充你的自定义对象的列表,那么你需要做这样的事情:

public List<Company> getCompanies() throws SQLException
{
    List<Company> result = new ArrayList<Company>();

    Connection connection = DriverManager.getConnection(...);
    Statement statement = connection.createStatement();
    ResultSet set = statement.executeQuery("your query here");

    set.beforeFirst();
    while(set.next())
    {
        String textData = set.getString("columnname");
        int integerData = set.getInt("columnname");

        // you can also use set.getString(columnindex);

        Company company = new Company();
        company.set(...);

        result.add(company);
    }
    connection.close();
    return result;
}
于 2012-08-21T10:28:51.487 回答
0

1-您应该使用创建与数据库的连接,

Class.forName(DB_LIBRARY);
connection = DriverManager.getConnection(CONNECTION_PARAMETRS);

请注意,您需要将 DB_LIBRARY 替换为有效的驱动程序库,并将 CONNECTION_PARAMETERS 替换为您的数据库连接设置(例如,主机名、用户名、密码等)。

2-创建一个语句,并返回一个结果集

Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(your_select_query);

3-遍历结果集并获取数据

Company company;
while(rs.next()){
         company = new Company(rs.getString(1), 
         rs.getString(2), 
         rs.getString(3), 
         rs.getInt(4),
         new HashMap<Integer, Integer>().put(rs.getInt(5), rs.getInt(6)
}

请注意,您可以使用列名称或它们的编号,如果您想获取多条记录,也可以使用 ArrayList,如下所示

ArrayList<Company> companies = new ArrayList<Company>();
while(rs.next()){
         companies.add(new Company(rs.getString(1), 
         rs.getString(2), 
         rs.getString(3), 
         rs.getInt(4),
         new HashMap<Integer, Integer>().put(rs.getInt(5), rs.getInt(6));
}
于 2012-08-21T10:55:17.843 回答