gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:43:17 24 4
gpt4 key购买 nike

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

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

如何获取真实的字符串数据? 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;
}
}

最佳答案

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

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

关于java - 如何使用java web服务从mysql数据库获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49923726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com