客户端代码段:
private void getServerInfo() {
try {
if (reader != null) {
list=(ArrayList<HashMap<String,Object>>)reader.readObject();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close(); // 关闭套接字
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器代码:
Connection conn = DAO.getConn();// 获得数据库连接
String sql="select sno,sname,sex from student";
Statement exet=conn.createStatement();
ResultSet res=exet.executeQuery(sql);
HashMap<String, Object> map;
ArrayList<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
while (res.next()) { // 如果套接字是连接状态
map=new HashMap<String,Object>();
map.put("id", res.getString("sno"));
map.put("sname", res.getString("sname"));
map.put("sex", res.getString("sex"));
list.add(map);
//list.add(map);
}
System.out.println(list.toString());
writer.writeObject(list);
writer.flush();
writer.close();
} catch (Exception e) {
ta_info.append("客户端已退出。\n"); |
|