如何向ArrayList添加ResultSet的数据?

时间:2015-07-14 13:42:49

标签: java arraylist resultset

我希望ArrayList拥有PostgreSQL数据库中的数据。

例如:

Column1   Column2   Column3   Column4
A         B         C         D
J         D         S         E

arraylist.get(0) = (A, B, C, D)
arraylist.get(1) = (J, D, S, E)

我有这个:

public static ArrayList<Ordenes> SelectInstruct (int a) throws SQLException
    {
        String driver = "org.postgresql.Driver";
        String server = "jdbc:postgresql://localhost:5432/postgres";
        String user = "usuario";
        String pass = "contraseña";

        ArrayList<Ordenes> ordenes = new ArrayList<Ordenes>();
        Ordenes ord = new Ordenes();

        try
        {
            Class.forName(driver);

            Connection conexion = DriverManager.getConnection(server, user, pass);
            Statement dato = conexion.createStatement();

            ResultSet rs = dato.executeQuery("SELECT * FROM \"Ordenes\"" + "WHERE \"Operario\" = "+a+";");

            conexion.close();
            rs.

            if (rs.next())
            {
                ord.setNumOrden(rs.getString("Numero de orden"));
                ord.setStatus(rs.getString("Status"));
                ord.setFechaInicio(rs.getString("Fecha Inicio"));
                ord.getPrd().setNumero(rs.getInt("Numero de producto"));
                ord.getPrd().setDescripcion(rs.getString("Descripcion Producto"));
                ord.setCantidad(rs.getInt("Cantidad Solicitada"));
                ord.setDescripcion(rs.getString("Descripcion Orden"));
                ord.getPrd().getInstr().get(0).setCodIns(rs.getInt("Codigo Instruccion")); 
                ord.getPrd().getInstr().get(0).setMat(rs.getInt("Material"));
                ord.getOper().add(rs.getInt("Operario"));

            }

            ordenes.add(ord);


        }
        catch (Exception e)
        {

        }

        return ordenes;

    }

我可以插入WhileFor吗?

2 个答案:

答案 0 :(得分:0)

我建议你创建一个列表列表。

List<List<String>> contents = new ArrayList<List<String>>();

    for(int i = 0; i < 10; i++)
    {
        List<String> columns = new ArrayList<String>();
        for(int j = 0; j < 4; j ++)
        {
            columns.add(j + "");
        }
        contents.add(columns);
    }
    System.out.println(contents.get(0));

输出

[0, 1, 2, 3]

将此问题应用于您的问题

List<List<String>> contents = new ArrayList<List<String>>();

    while(rs.next())
    {
        List<String> columnData = new ArrayList<String>();
        columnData.add(rs.getString("name"));
        columnData.add(rs.getString("email"));
        columnData.add(rs.getString("phone"));
        columnData.add(rs.getString("fax");
        //Add all your data to the contents ArrayList
        contents.add(columns);
    }
    System.out.println(contents.get(0));

答案 1 :(得分:0)

如果你输出更多结果,请使用while循环,如下面的代码。我只是改变了。

public static ArrayList<Ordenes> SelectInstruct (int a) throws SQLException
    {
        String driver = "org.postgresql.Driver";
        String server = "jdbc:postgresql://localhost:5432/postgres";
        String user = "usuario";
        String pass = "contraseña";

        ArrayList<Ordenes> ordenes = new ArrayList<Ordenes>();


        try
        {
            Class.forName(driver);

            Connection conexion = DriverManager.getConnection(server, user, pass);
            Statement dato = conexion.createStatement();

            ResultSet rs = dato.executeQuery("SELECT * FROM \"Ordenes\"" + "WHERE \"Operario\" = "+a+";");

            conexion.close();
            rs.

            while(rs.next())
            {
                Ordenes ord = new Ordenes();
                ord.setNumOrden(rs.getString("Numero de orden"));
                ord.setStatus(rs.getString("Status"));
                ord.setFechaInicio(rs.getString("Fecha Inicio"));
                ord.getPrd().setNumero(rs.getInt("Numero de producto"));
                ord.getPrd().setDescripcion(rs.getString("Descripcion Producto"));
                ord.setCantidad(rs.getInt("Cantidad Solicitada"));
                ord.setDescripcion(rs.getString("Descripcion Orden"));
                ord.getPrd().getInstr().get(0).setCodIns(rs.getInt("Codigo Instruccion")); 
                ord.getPrd().getInstr().get(0).setMat(rs.getInt("Material"));
                ord.getOper().add(rs.getInt("Operario"));

            }

            ordenes.add(ord);


        }
        catch (Exception e)
        {
          // handle the exception here 
        }

        return ordenes;

    }