slideshow.jsp
上传用户:shjgzm
上传日期:2017-08-31
资源大小:2757k
文件大小:2k
源码类别:

Ajax

开发平台:

Java

  1. <%@ page contentType="text/plain; charset=UTF-8"%>
  2. <%@ page language="java"%>
  3. <%@ page import="java.sql.*,ajax.db.DBUtils"%>
  4. <%
  5.     out.clear();                                    //清空当前的输出内容(空格和换行符)
  6.     String sql = "select * from album";             //定义查询数据库的SQL语句
  7.     StringBuffer picInfo = new StringBuffer("[");   //保存查询结果
  8.     int counter = 0;                                //计数器
  9.     Connection conn = null;                 //声明Connection对象
  10.     PreparedStatement pstmt = null;         //声明PreparedStatement对象
  11.     ResultSet rs = null;                    //声明ResultSet对象
  12.     try {
  13.         conn = DBUtils.getConnection();     //获取数据库连接
  14.         pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
  15.         rs = pstmt.executeQuery();          //执行查询,返回结果集
  16.         //遍历结果集,生成JSON格式的查询结果
  17.         while (rs.next()) {
  18.             if (counter > 0) {
  19.                 picInfo.append(",");
  20.             }
  21.             picInfo.append("{");
  22.             picInfo.append("'id':'");
  23.             picInfo.append(rs.getString("id"));
  24.             picInfo.append("','name':'");
  25.             picInfo.append(rs.getString("name"));
  26.             picInfo.append("','width':'");
  27.             picInfo.append(rs.getString("width"));
  28.             picInfo.append("','height':'");
  29.             picInfo.append(rs.getString("height"));
  30.             picInfo.append("'}");
  31.             counter++;                      //计数器加1
  32.         }
  33.     } catch (SQLException e) {
  34.         System.out.println(e.toString());
  35.     } finally {
  36.         DBUtils.close(rs);                  //关闭结果集
  37.         DBUtils.close(pstmt);               //关闭PreparedStatement
  38.         DBUtils.close(conn);                //关闭连接
  39.     }
  40.     picInfo.append("]");
  41.     out.print(picInfo.toString());          //返回查询结果
  42. %>