Results 1 to 6 of 6
  1. #1
    dejoere is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default passing arraylist

    hello, i am making an application about a restaurant, and i get the arraylist form an other class, so i can use it in my databaseconnection can u plzz help me i really dont know what to do anymore


    this is my database connection where i need the arraylist to go to (in the vraagGang() )
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;


    public class DbConnect
    {
    private Restaurant restaurant;

    public DbConnect(Restaurant cRestaurant)
    {
    restaurant = cRestaurant;

    }

    public ArrayList<Ingredient> vraagIngredient()
    {
    ArrayList<Ingredient> ingredient = new ArrayList<Ingredient>();
    Connection c = null;;
    Statement s=null;
    ResultSet rs=null;

    try
    {

    c= DriverManager.getConnection("jdbc:odbc:Project");
    s = c.createStatement();
    rs = s.executeQuery("SELECT * FROM Ingredient");




    while(rs.next())
    {
    Ingredient i = new Ingredient(rs.getInt("Id"),rs.getString("Naam"), rs.getString("Eenheid"),rs.getDouble("Prijs"));
    ingredient.add(i);
    }


    }
    catch (SQLException e)
    {
    System.out.print(e);
    }
    finally
    {

    try {
    rs.close();
    s.close();
    c.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }


    }
    return ingredient;
    }
    public ArrayList<Klant> vraagKlant()
    {
    ArrayList<Klant> Klant = new ArrayList<Klant>();
    Connection c=null;
    Statement s=null;
    ResultSet rs=null;

    try
    {

    c= DriverManager.getConnection("jdbc:odbc:Project");
    s = c.createStatement();
    rs = s.executeQuery("SELECT * FROM Klant");




    while(rs.next())
    {
    Klant i = new Klant(rs.getInt("Klantnummer"),rs.getString("Naam" ),rs.getString("Straat"),rs.getString("Huisnummer" ),rs.getInt("Postcode"),rs.getString("Gemeente"),r s.getInt("Telefoonnummer"),rs.getString("E-mail"));
    Klant.add(i);
    }


    }
    catch (SQLException e)
    {
    System.out.print(e);
    }
    finally
    {

    try {
    rs.close();
    s.close();
    c.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }

    }
    return Klant;
    }
    public ArrayList<Gang> vraagGang()
    {
    Restaurant lalampada = restaurant;
    ArrayList<Gang> gang = new ArrayList<Gang>();
    ArrayList<Benodigdheid> benodigdheid = new ArrayList<Benodigdheid>();
    Connection c = null;

    try
    {
    c = DriverManager.getConnection("jdbc:odbc:Project");
    Statement s = c.createStatement();
    ResultSet rs = s.executeQuery("SELECT * FROM Gang");

    while(rs.next())
    {

    Gang g = new Gang(rs.getInt("Id"), rs.getString("Naam"),benodigdheid, rs.getDouble("Werkkost"));

    PreparedStatement prep = c.prepareStatement("SELECT Ingredient_ID, Hoeveelheid, Eenheid FROM Benodigdheden WHERE GangID = ?");
    prep.setInt(1, g.getId());
    ResultSet rs2 = prep.executeQuery();// 2 parameters expeceted ???? wtf is da?


    while (rs2.next())
    {

    int Id = rs2.getInt(1);

    ArrayList<Ingredient> ingredient = lalampada.getIngredient();
    Ingredient tijdelijk = null;
    for(Ingredient i : ingredient)
    {
    if(i.getIngreId() == Id)
    {
    tijdelijk = i;
    }
    }


    Benodigdheid b = new Benodigdheid(tijdelijk, rs2.getInt("hoeveelheid"),rs2.getString("Eenheid") );
    benodigdheid.add(b);
    }
    g.getBenodigdheid();// klopt niet denk ik
    gang.add(g);
    }
    return gang;
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    }
    finally
    {
    try
    {
    c.close();
    }
    catch(SQLException e)
    {
    e.printStackTrace();
    }
    }
    return gang;
    }

    public ArrayList<Menu> vraagMenu()// maakt gebruik van gang (hoe krijg ik gang terug van het hoofdprogramma?????)
    {
    ArrayList<Menu> menu = new ArrayList<Menu>();
    Connection c = null;;
    Statement s=null;
    ResultSet rs=null;
    try
    {

    c= DriverManager.getConnection("jdbc:odbc:Project");
    s = c.createStatement();
    rs = s.executeQuery("SELECT * FROM Reservatie");// alle reservatie ophalen
    }
    catch (SQLException e)
    {
    System.out.print(e);
    }
    finally
    {

    try {
    rs.close();
    s.close();
    c.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }

    }




    return menu;
    }
    public ArrayList<Reservatie> vraagReservatie()
    {
    ArrayList<Reservatie> reservatie = new ArrayList<Reservatie>();
    Connection c = null;;
    Statement s=null;
    ResultSet rs=null;
    ResultSet rs1=null;
    ResultSet rs2=null;

    try
    {

    c= DriverManager.getConnection("jdbc:odbc:Project");
    s = c.createStatement();
    rs = s.executeQuery("SELECT * FROM Reservatie");// alle reservatie ophalen




    while(rs.next())
    {
    rs1 =s.executeQuery("SELECT * FROM Klant WHERE Klantnummer ="+rs.getInt("Klant"));// klanten die bij een reservatie horen ophalen(gaat altijd maar 1 klant zijn)
    rs2 =s.executeQuery("SELECT * FROM Menu WHERE Id ="+rs.getInt("Menu_Id"));//menu die bij een reservatie hoort opdhalen(gaat altijd maar 1 menu zijn)
    Klant i = new Klant(rs1.getInt("Klantnummer"),rs1.getString("Naa m"),rs1.getString("Straat"),rs1.getString("Huisnum mer"),rs1.getInt("Postcode"),rs1.getString("Gemeen te"),rs1.getInt("Telefoonnummer"),rs1.getString( "E-mail"));
    //Menu b = new Menu();

    }


    }
    catch (SQLException e)
    {
    System.out.print(e);
    }
    finally
    {

    try {
    rs.close();
    rs1.close();
    s.close();
    c.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }

    }
    return reservatie;

    }

    }
    this is the class where i need to get the arraylist from
    import java.util.ArrayList;



    public class Restaurant
    {
    private ArrayList<Menu> menu;
    private ArrayList<Gang> gang;
    private ArrayList<Ingredient> ingredient;
    private ArrayList<Klant> klanten;
    private ArrayList<Reservatie> reservatie;
    private ArrayList<Benodigdheid> benodigdheid;
    private DbConnect db;

    public Restaurant()
    {
    db = new DbConnect(this);
    ingredient = db.vraagIngredient();
    klanten = db.vraagKlant();
    //gang = db.vraagGang();// hier zit ook een fout in
    }

    public ArrayList<Menu> getMenu()
    {

    return menu;
    }

    public void setMenu(ArrayList<Menu> menu) {
    this.menu = menu;
    }

    public ArrayList<Gang> getGang() {
    return gang;
    }

    public void setGang(ArrayList<Gang> gang) {
    this.gang = gang;
    }

    public ArrayList<Ingredient> getIngredient()
    {

    return ingredient;
    }

    public void setIngredient(ArrayList<Ingredient> ingredient) {
    this.ingredient = ingredient;
    }

    public ArrayList<Klant> getKlanten() {
    return klanten;
    }

    public void setKlanten(ArrayList<Klant> klanten) {
    this.klanten = klanten;
    }

    public ArrayList<Reservatie> getReservatie() {
    return reservatie;
    }

    public void setReservatie(ArrayList<Reservatie> reservatie) {
    this.reservatie = reservatie;
    }

    public ArrayList<Benodigdheid> getBenodigdheid() {
    return benodigdheid;
    }

    public void setBenodigdheid(ArrayList<Benodigdheid> benodigdheid) {
    this.benodigdheid = benodigdheid;
    }



    }
    this is my class Gang
    import java.util.ArrayList;

    public class Gang
    {
    //attributen
    private int id;
    private String naam;
    private ArrayList<Benodigdheid> benodigdheid;
    private double werkkost;


    //constructor
    public Gang (int cId,String cNaam,ArrayList<Benodigdheid> cBenodigdheid, double cWerkkost)
    {
    setId(cId);
    setNaam (cNaam);
    setBenodigdheid (cBenodigdheid);
    setWerkkost (cWerkkost);
    }

    //accessors
    public void setId(int mId)
    {
    id = mId;
    }
    public void setNaam(String mNaam)
    {
    naam = mNaam;
    }

    public void setBenodigdheid( ArrayList<Benodigdheid> mBenodigdheid)
    {
    benodigdheid = mBenodigdheid;
    }

    public void setWerkkost(double mWerkkost)
    {
    werkkost = mWerkkost;
    }


    //mutators
    public int getId()
    {
    return id;
    }
    public String getNaam ()
    {
    return naam;
    }

    public ArrayList<Benodigdheid> getBenodigdheid()
    {
    return benodigdheid;
    }

    public double getWerkkost()
    {
    return werkkost;
    }
    // andere methodes
    public double berekenKostprijs()
    {
    double kostprijs = 5;//tijdelijk !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!
    return kostprijs;
    }

    }
    this is my class Benodigdheid (that is used in gang)
    public class Benodigdheid
    {
    private Ingredient ingredient;
    private int hoeveelheid;
    private String eenheid;


    public Benodigdheid(Ingredient ingredienten, int cHoeveelheid,String cEenheid)
    {

    setIngredient(ingredienten);
    setHoeveelheid(cHoeveelheid);
    setEenheid(cEenheid);
    }

    //accessors

    public Ingredient getIngredient()
    {
    return ingredient;
    }

    public int getHoeveelheid()
    {
    return hoeveelheid;
    }
    public String getEenheid()
    {
    return eenheid;
    }

    //mutators

    public void setIngredient(Ingredient mIngredient)
    {

    }

    public void setHoeveelheid(int mHoeveelheid)
    {

    }
    public void setEenheid(String mEenheid)
    {

    }
    public void berekenAankoopLijst ()
    {

    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Can you call a method in the other class (the one with the arraylist) and have it return the arraylist to its caller?

    Something like:
    Java Code:
    ArrayList getArrayList() {
       return theArrayList;
    }

  3. #3
    dejoere is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    public ArrayList<Ingredient> getIngredient()
    {

    return ingredient;
    }
    i have that, but how can i get the arraylist in the class DbConnect ?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Where does the arraylist exist? What class is it in?
    Have the DbConnect class call the getIngredient method of the class containing the arraylist.

  5. #5
    dejoere is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    the arraylist exists in Restaurant, restaurants gets the arraylist from Dbconnect
    import java.util.ArrayList;



    public class Restaurant
    {
    private ArrayList<Menu> menu;
    private ArrayList<Gang> gang;
    private ArrayList<Ingredient> ingredient;
    private ArrayList<Klant> klanten;
    private ArrayList<Reservatie> reservatie;
    private ArrayList<Benodigdheid> benodigdheid;
    private DbConnect db;

    public Restaurant()
    {
    db = new DbConnect(this);
    ingredient = db.vraagIngredient();
    klanten = db.vraagKlant();
    //gang = db.vraagGang();// hier zit ook een fout in
    }
    so i think this is the place where the arraylist exists, but my question is how do i get it into my dbconnect ??

    in the class dbconnect i have
    public class DbConnect
    {
    private Restaurant restaurant;

    public DbConnect(Restaurant cRestaurant)
    {
    restaurant = cRestaurant;

    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Did you understand what I suggested before?
    Add a method to the Restaurant class that returns the arraylist.
    In the DbConnect class, add a call to that method.

Similar Threads

  1. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 07:07 AM
  2. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 12:48 PM
  3. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  4. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  5. Help passing arraylist to another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 09:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •