将字符串显示为字符串

时间:2016-02-21 17:09:28

标签: java arraylist

我创建了一个充满了数据库表中记录的arraylist。当我去显示arraylist的内容时,输出是不可读的。我怎么能纠正这个?提前谢谢。

输出

[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);



		
	}
}
	
	

	

更新。已完成。

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;
	  }

	
	

}

3 个答案:

答案 0 :(得分:1)

您要打印的列表有两个Main个对象,但是您没有覆盖此类的toString(),因此您将获得默认实现,即类名+'@'+ hashCode ()对象。

如果您需要其他toString(),则需要提供重写方法。

e.g

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

我的猜测是你实际上并不想将Main个对象添加到列表中,而是应该添加从数据库中返回的日期。 e.g。

dates.add(rst.getTimestamp(1));

答案 1 :(得分:1)

覆盖toString方法会有所帮助。例如:

public String toString(){
    return "Main"; //Give some useful value here
}

答案 2 :(得分:0)

让我们在您的Main类中说,您有2个字段和字段的getter和setter:

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;
}
}

现在你只需要覆盖同一个类中的toString()方法:

@Override
public String toString() {
    return "Main [name=" + name + ", number=" + number + "]";
}