java 怎么把多个对象写进同一个文件里,用集合 java 怎么把多个对象写进同一个文件里,用集合 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 定义个List啊! List l=new ArrayList();l.add(object); public List<Employee> findAll() throws SQLException { /* * 用DB工具类连接数据库,用PreparedStatement()方法将sql语句发送到数据库 * 用executeQuery()方法执行操作 */ Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(FINDALL); ResultSet rs = ps.executeQuery(); List<Employee> employees = new ArrayList<Employee>(); /* * 用while循环sql语句的结果,并把它添加到 employees集合中 */ while(rs.next()){//rs是结果集,迭代 将rs全部进行读取 Employee e = new Employee(); e.setId(rs.getLong("id")); e.setEname(rs.getString("ename")); e.setGender(rs.getString("gender")); e.setAge(rs.getInt("age")); e.setSalary(rs.getDouble("salary")); e.setDeptno(rs.getLong("deptno")); employees.add(e); } //关闭连接 DBUtil.close(conn); return employees; } 用ObjectOutputStream把对象写入文件中。。如果是多个对象,可以试试把对象add到HashSet或ArrayList中。。 FileOutputStream fos = new FileOutputStream("e:\\xxx"); ObjectOutputStream oos = new ObjectOutputStream(fos); Set set = new HashSet(); Student s1 = new Student(); Student s2 = new Student(); set.add(s1); set.add(s2); oos.writeObject(set); oos.close(); fos.close(); webserice拒绝访问:FileNotFoundExceptpion Log4j当日志到达一定大小重新生成文件的问题 邪门了。wml的表单获取不到中文 我是大三非计算机专业学生,提问有关j2ee学习? hibernate search 的问题,求助高手! tomcat5.0不自动部署war包 我的程序連接MSSQL2000,JDBC-ODBC的問題﹗ 关于JBoss 页面提交过期问题 SSH 使用hibernate查询添加遇到类型转换的问题??非常不解 在ssh框架下如何把下述的.java文件中的内容显示在网页中??? 和大家讨论一个编码的问题 求解?
List l=new ArrayList();
l.add(object);
public List<Employee> findAll() throws SQLException {
/*
* 用DB工具类连接数据库,用PreparedStatement()方法将sql语句发送到数据库
* 用executeQuery()方法执行操作
*/
Connection conn = DBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(FINDALL);
ResultSet rs = ps.executeQuery();
List<Employee> employees = new ArrayList<Employee>();
/*
* 用while循环sql语句的结果,并把它添加到 employees集合中
*/
while(rs.next()){//rs是结果集,迭代 将rs全部进行读取
Employee e = new Employee();
e.setId(rs.getLong("id"));
e.setEname(rs.getString("ename"));
e.setGender(rs.getString("gender"));
e.setAge(rs.getInt("age"));
e.setSalary(rs.getDouble("salary"));
e.setDeptno(rs.getLong("deptno"));
employees.add(e);
}
//关闭连接
DBUtil.close(conn);
return employees;
}
FileOutputStream fos = new FileOutputStream("e:\\xxx");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Set set = new HashSet();
Student s1 = new Student();
Student s2 = new Student();
set.add(s1);
set.add(s2);
oos.writeObject(set);
oos.close();
fos.close();