I have this 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:

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 class (CarShop) in some readable form?

Please post the output you are seeing.

If it's the Car class that is giving the hex codes, add a toString method (override the Object class's toString method) to the Car class that returns the String you want to see.

You were right. It is the Car class that's represented as hash, and I overrode its toString method. More or less, I got what I wanted, but I would like to format output to look more descriptive and clear,like returning one object(class) below other (using toString method). Is it possible?

Please show what you want the output to look like.
If you want different parts of the contents of the Car object on different lines, you could add getThis and getThat methods to the class that you could use to get the different data you want on each line.
Another way would be to use "\n" in the String returned by toString.

I used "/n" and it looks the way I wanted. Thanks, Norm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.