如何使用java Web服务从mysql数据库获取数据?

时间:2018-04-19 14:33:49

标签: java mysql web-services soap

我尝试从mysql数据库获取数据并使用java web服务 SOAP REQUEST 存储在Arraylist中。但我的方法返回输出就像这样 java.util.List:" [chat.in.server.com.ThreadsClass@17b91ad3

ThreadClass是处理线程信息的类。线程意味着消息

如何获得真正的String数据? enter image description here

线程类

public class ThreadsClass  {

private int id;
private String title;
private String author;
private String date;


public ThreadsClass() {
}

public ThreadsClass(int id,String title, String author, String date) {
    this.id=id;
    this.title = title;
    this.author = author;
    this.date = date;
}

public String getTitle() {
    return title;
}

public String getAuthor() {
    return author;
}

public String getDate() {
    return date;
}

public int getId() {
    return id;
}
}

Java Web服务类

@WebService(serviceName = "ChatHandle")

public class ChatHandle {

private Connection connection;
private Statement statement;
private ResultSet resultsSet;
private PreparedStatement prepStat;

@WebMethod(operationName = "displayThread")
public ArrayList<ThreadsClass> displayThread() {
       ArrayList<ThreadsClass> threads = new ArrayList<>();
    try {
         Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/chatapp", "root", "");
        statement = connection.createStatement();
        resultsSet = statement.executeQuery("select id,title, date, author from chatnew");
        while (resultsSet.next()) {
            ThreadsClass t = new ThreadsClass(
            resultsSet.getInt("id"),
            resultsSet.getString("title"),
            resultsSet.getString("author"),
            resultsSet.getString("date"));
            threads.add(t);
        }
    } catch (Exception e) {
 e.printStackTrace(new PrintStream(System.out));
    }
    return threads;
}
}

1 个答案:

答案 0 :(得分:0)

Java打印实例哈希码作为将对象转换为字符串的默认行为。尝试在ThreadsClass类中实现toString()

@Override
public String toString(){
  return "ThreadsClass: title = " + title + " author = " + author + " date = " + date + " id = " + id;
}