日期:2014-05-19 浏览次数:20581 次
Connection conn = DBConnection.getConnection(); //获得连接对象 String findSQL = "select * from tb_message " + "order by publishTime desc limit ?,?"; PreparedStatement pstmt = null; //声明预处理对象 ResultSet rs = null; List<Message> messages = new ArrayList<Message>(); try { pstmt = conn.prepareStatement(findSQL); //获得预处理对象并赋值 pstmt.setInt(1, page.getBeginIndex()); //查询起始点 pstmt.setInt(2, page.getEveryPage()); //查询记录数 rs = pstmt.executeQuery(); //执行查询 while(rs.next()) { Message message = new Message(); message.setMessageID(rs.getInt(1)); //设置消息ID message.setMessageTitle(rs.getString(2));//设置消息标题 message.setMessageContent( rs.getString(3)); //设置消息内容 message.setEmployeeID(rs.getInt(4));//设置员工编号 message.setPublishTime(rs.getTimestamp(5));//设置发布时间 messages.add(message);//添加消息 } } catch (SQLException e) { e.printStackTrace(); } finally{ DBConnection.close(rs); DBConnection.close(pstmt); DBConnection.close(conn); }