Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jdbc连接将两个表映射到单个arraylist对象_Java_Sql_Jdbc_Collections_Arraylist - Fatal编程技术网

Java 使用jdbc连接将两个表映射到单个arraylist对象

Java 使用jdbc连接将两个表映射到单个arraylist对象,java,sql,jdbc,collections,arraylist,Java,Sql,Jdbc,Collections,Arraylist,我有两个表,即SALE和SALEPRODUCT(这两个表之间有一对多关系()。 SALE表包含单个销售的详细信息,SALEPRODUCT包含与该销售相关的所有产品详细信息。 我已经取得了所有的销售和他们各自的产品细节。 I hv有两个类sale和saleProduct。这两个类都有自己的变量和这些变量的属性。 我想获取如下示例所示的数据 List<Data> sale= new ArrayList<Data>(); List<SubData> s

我有两个表,即SALE和SALEPRODUCT(这两个表之间有一对多关系()。 SALE表包含单个销售的详细信息,SALEPRODUCT包含与该销售相关的所有产品详细信息。 我已经取得了所有的销售和他们各自的产品细节。 I hv有两个类sale和saleProduct。这两个类都有自己的变量和这些变量的属性。 我想获取如下示例所示的数据

 List<Data> sale= new ArrayList<Data>();
      List<SubData> saleProduct= new ArrayList<SubData>();
      subData.add(new saleProduct(2011, 0, 10, 2));
      subData.add(new saleProduct(2011, 0, 15, 3));
      data.add(new Data("DVD", 5, new BigDecimal(30), subData));
      saleProduct= new ArrayList<SubData>();
      subData.add(new saleProduct(2011, 0, 11, 1);
      subData.add(new saleProduct(2011, 0, 12, 3);
      subData.add(new saleProduct(2011, 0, 16, 4));
      data.add(new Data("Book", 8, new BigDecimal(11), subData));
      saleProduct= new ArrayList<SubData>();
      subData.add(new saleProduct(2011, 0, 9, 1));
      subData.add(new saleProduct(2011, 0, 18, 1));
      data.add(new Data("PDA", 2, new BigDecimal(15), subData));
List sale=new ArrayList();
List saleProduct=new ArrayList();
子数据。添加(新销售产品(2011,0,10,2));
子数据。添加(新销售产品(2011,0,15,3));
添加(新数据(“DVD”,5,新的大十进制(30),子数据));
saleProduct=newarraylist();
子数据。添加(新销售产品(2011,0,11,1);
子数据。添加(新销售产品(2011,0,12,3);
子数据。添加(新销售产品(2011,0,16,4));
添加(新数据(“账簿”,8,新的大十进制(11),子数据));
saleProduct=newarraylist();
子数据。添加(新销售产品(2011,0,9,1));
子数据。添加(新销售产品(2011,0,18,1));
添加(新数据(“PDA”,2,新的大十进制(15),子数据));
它是一个硬编码的数据,但我需要来自数据库(来自两个表)的数据。
如何从这两个表中获取数据并创建单个列表、集合或ArrayList对象。

执行SQL,因为您不使用任何ORM框架这是我所能想到的方法

SELECT *
FROM SALES s,
     SALEPRODUCT sp 
WHERE s.id=sp.id; 
这里我假设id是 两个表中的公共列(用于维护关系)。然后您将获得一个包含所有必要结果的列表,获取每个结果并从中创建类对象,然后将其添加到集合或列表中

示例代码:-

public class MyCustomEntity {
  // Here add all the attributes that you get from your query
  // Add getters and setters for all those attributes.
}
现在,当您获得结果时,将创建此MyCustomEntity类的新实例

while(some condition) {

MyCustomEntity mce = new MyCustomEntity();
mce.setXXX(pass your parameter from result here);
Set<MyCustomEntity> mySet = new HashSet<MyCustomEntity>();
mySet.add(mce);

}
while(某些条件){
MyCustomEntity mce=新的MyCustomEntity();
mce.setXXX(在此处传递结果中的参数);
Set mySet=newhashset();
mySet.add(mce);
}

因此,创建您的集合。

在SQL中执行内部联接获取并将其映射到您的objectsQuery不是我的问题。如何将此结果存储到对象中?如果您得到的结果包含所有这些参数,则只需在新创建的对象上应用setter方法,然后将其添加到您想要的列表/集合中。如果我的答案合适,请回答对你来说,请接受它并结束问题。