CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2011
    Posts
    15

    PrintArrayListFromMainClass

    I have this code:
    Code:
    import java.sql.*;
    import java.util.ArrayList;
    
    public class Car {
    private int id;
    private String model;
    private String color;
    private Double price;
    public void setId(int id) { this.id = id; }
    public int getId() { return this.id; }
    public void setModel(String model) { this.model = model; }
    public String getModel() { return this.model; }
    public void setColor(String color) { this.color = color; }
    public String getColor() { return this.color; }
    public void setPrice(double price) { this.price = price; }
    public double getPrice() { return this.price; }
    public Car(int id, String model, String color, Double price)
    {
    
        this.id = id;
        this.model = model;
        this.color = color;
        this.price = price;
      }
    
    
    public static ArrayList getAllCars()
    {
    Connection conn = null;
    ArrayList carList = new ArrayList();
    {
    try
        {
            
          Class.forName ("com.mysql.jdbc.Driver").newInstance ();
              conn = DriverManager.getConnection ("jdbc:mysql://localhost/carShopDB", "root", "");
            System.out.println(conn.isClosed());
        }
    catch(Exception ex)
    { 
    System.out.println("Connection not established"); 
    
    }
     try
    {
        Statement st = conn.createStatement();
        st.executeQuery("select * from cars");
        ResultSet rs = st.getResultSet();
              while(rs.next())
        {
          carList.add(new Car(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getDouble(4)));
          }
        }
    
        catch (SQLException s)
       {
      System.out.println("SQL statement not executed!");
    }
    finally {
    try
        {
        if(conn!=null && !conn.isClosed())
            conn.close();
        System.out.println(conn.isClosed());
        }
    catch(Exception ex) { }
    return carList;
    
     }
       
    }
    
    }
    
    }
    I want to print this ArrayList from main class, and I used this:
    Code:
    public class CarShop {
    public static void main(String[] args) {
            System.out.println(Car.getAllCars());
            }
    }
    but it returns list of objects represented as hash code.
    How can I print this arraylist from main in some readable form?
    Last edited by nera1981; August 28th, 2011 at 03:06 AM.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: PrintArrayListFromMainClass

    See your post on another forum where I answered this question.
    Norm

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: PrintArrayListFromMainClass

    you can do it two ways System.out.println(arraylist) is overloaded to print contents of an array list. or
    Code:
    for(int a=0; a<arraylist.size();a++)
    {
     arraylist.get(a)  // don't for get to cast it relevant   object 
    
    }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured