所以我有这个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=?"
'cats'和'yes'没有任何意义,只是我极其详细的命名方案的胜利。
公司只是一个字符串。
它返回的表格如下所示:
---------------------------------------------
|companyName|surveyID|questionID|questionScore|
---------------------------------------------
此表的主键是(companyName,surveyID和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;
}
拥有所有吸气剂和制定者
例如,假设有一家公司Mens Insamen。 SurveyID是1。 所以
Mens Insamen 1 q1 3
Mens Insamen 1 q2 1
等...
现在我需要使用sql查询中的数据填充List of Company对象。我已经尝试了一段时间,但无法想出任何东西。
如果(不出所料)不清楚(fokol(no)咖啡)我可以在某些时候改进它。
提前致谢
答案 0 :(得分:0)
你可以简单地使用arraylist和你的while循环来实现这一点 创建你的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);
}
答案 1 :(得分: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;
}
答案 2 :(得分: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;
}
答案 3 :(得分: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));
}