Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JDBC and Relational Databases
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
JDBC and Relational Databases
PrintArrayListFromMainClass
nermin vucinic
Greenhorn
Posts: 9
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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 in some readable form?
Matthew Brown
Bartender
Posts: 4568
9
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You need to iterate across the ArrayList, printing out the details you want from each entry.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
ejbLoad is not getting called
Problems with Bean in JSP...
How to return class instance?
comparing using arrays in java
returning an int
More...