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?