-1
我需要從數據庫(MySQL)讀取數據並傳輸到客戶端。客戶端 - 服務器。如何轉移到客戶端,一個自定義類?
我可以以某種方式將TopickOfVote
類型的對象轉移到客戶端或僅通過字符串? 作者與讀者:
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();
}
}
從被你得到'writer'和'reader'?如果您必須使用它們,那麼您需要使用文本序列化(例如,可以使用JSON)。如果你有'InputStream'並且可以創建'TopicOfVote'' Serializable',那麼你可以使用'ObjectInputStream'和''ObjectOutputStream''。 – Oleg
謝謝!奇怪。但是,之前,它不適用於'ObjectInputStream'和'ObjectOutputStream' –
'它沒有工作'不是一個問題描述。如果這就是你想問的問題,請正確地問。 – EJP