我需要从数据库(MySQL)读取数据并传输到客户端。
我可以以某种方式将TopickOfVote
类型的对象传输到客户端或仅通过String传输吗?
作家和读者:
writer = new PrintWriter(new OutputStreamWriter(clientAPP.getWriter()));
reader = new BufferedReader(new InputStreamReader(clientAPP.getReader()));
服务器:
private void LoadOfTopics() throws SQLException {
resultSet = statement.executeQuery("SELECT * FROM kursach.TopicOfVote");
StringBuffer id;
StringBuffer topic;
while(resultSet.next()){
TopicOfVote topicOfVote = new TopicOfVote();
topicOfVote.setId(Integer.parseInt(resultSet.getString("idVote")));
topicOfVote.setTopic(resultSet.getString("topic"));
ResultSet ans = statement.executeQuery("SELECT * FROM kursach.Answers_vote WHERE idVote=" + topicOfVote.getId());
ArrayList<Answer> answers = new ArrayList<>();
while (ans.next()){
Answer answer = new Answer();
answer.setId(Integer.parseInt(ans.getString("idAnswer")));
answer.setAnswer(ans.getString("answer"));
answer.setIdOfTopic(Integer.parseInt(ans.getString("idVote")));
answers.add(answer);
}
topicOfVote.setAnswers(answers);
writer.println(topicOfVote); // ???
writer.flush();
ans.close();
}
}
客户端:
private void createPanelTopics(){
try {
writer.println(HandlerQueries.GET_VOTES + "?");
writer.flush();
reader.readLine();//?????
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
我可以以某种方式将TopickOfVote类型的Object传输到客户端
是的,使用对象序列化,TopickOfVote
实现Serializable
并执行所有其他相关的事情,例如定义private static final long serialVersionUID
,并且不会进行不兼容的演变。< / p>
还是只通过String?作家和读者:
是的,使用XML,JSON,...