0

I have created an arraylist that is filled with records from a database table. When I go to display the contents of the arraylist, the output is not readable. How could I correct this? Thanks in advance.

Output

[arraylistTest.Main@6433a2, arraylistTest.Main@110e440]

package arraylistTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {

 public static void main(String[] args) throws SQLException, ClassNotFoundException {
   Connection con = null;
         Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/books", "user", "peter");
      Statement stm;
      stm = con.createStatement();
      String sql = "Select Date from abooked";
      ResultSet rst;
      rst = stm.executeQuery(sql);
      ArrayList<Main> dates = new ArrayList<>();
      while (rst.next()) {
          Main list = new Main();
          dates.add(list);
      }

      System.out.println(dates);



  
 }
}
 
 

 

UPDATE. COMPLETED.

Main.java

package query;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;
import object.Dates;

public class ReadQuery {
  // static StudentPojo sPojo;
  /**
   * @param args
   */
  public static void main(String[] args) {
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
    String dbName = "Booking";
    String uname = "";
    String pwd = "";
    String url = "jdbc:mysql://localhost:3306/" + dbName;


    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con = DriverManager.getConnection(url, uname, pwd);
      System.out.println("Connection Established: " + con);
      String qry = "select bookedDate from appointment_booked";

      st = con.createStatement();
      rs = st.executeQuery(qry);

      JSONObject jObj = new JSONObject();
      ArrayList < Dates > list = new ArrayList < Dates > ();
      Map < String, String > map = new HashMap < String, String > ();
      Dates sPojo = null;
      while (rs.next()) {
        sPojo = new Dates();

        sPojo.setDates(rs.getString("booked"));
        list.add(sPojo);

      }
      System.out.println(list);

      jObj.put("dates", list);

      System.out.println(jObj.toString());

    } catch (Exception e) {
      e.printStackTrace();
    }

  }

}

Dates.java

package object;

public class Dates {
 
 private String dates;

 public Dates() {
  
 }

 public String getDates() {
  return dates;
 }

 public void setDates(String dates) {
  this.dates = dates;
 }
 
 @Override
   public String toString() {
     return dates;
   }

 
 

}
abcdefg
  • 69
  • 9

3 Answers3

1

The list you are printing has two Main objects, however you haven't overridden the toString() for this class so you get the default implementation which is the class name + '@' + the hashCode() of the object.

If you want a different toString() you need to provide an overridden method.

e.g

public String toString() {
    return /* how I want Main to be printed */;
}

My guess is you dodn't actually want to add Main objects to the list, but instead you should be adding the Dates you get back from the database. e.g.

dates.add(rst.getTimestamp(1));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Overriding toString method will help. For example:

public String toString(){
    return "Main"; //Give some useful value here
}
Faraz
  • 6,025
  • 5
  • 31
  • 88
0

Lets say in your Main class you have 2 fields and getters and setters for fields:

public class Main{

private String name;
private int number;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getNumber() {
    return number;
}
public void setNumber(int number) {
    this.number = number;
}
}

Now you just need to override toString() method inside the same class:

@Override
public String toString() {
    return "Main [name=" + name + ", number=" + number + "]";
}
Faraz
  • 6,025
  • 5
  • 31
  • 88
  • You advise me to that this was a very bad example and tried to find the source for you but couldn't. I just researched and watched videos to understand the logic and syntax. Thank you Faraz. – abcdefg Feb 23 '16 at 19:32