[java]代码库
package ExamProject.console.servlet;
import <a target="_blank" href="http://www.myexception.cn/j2me/java.io.IOException.html">java.io.IOException</a>;
import java.io.PrintWriter;
import java.sql.ResultSet;
import <a target="_blank" href="http://www.myexception.cn/java-web/java.sql.SQLException.html">java.sql.SQLException</a>;
import <a target="_blank" href="http://www.myexception.cn/java-web/javax.servlet.ServletException.html">javax.servlet.ServletException</a>;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ExamProject.console.jdbc.JDBCConnection;
import net.sf.json.JSONArray;
public class SelectAllServer extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws <a target="_blank" href="http://www.myexception.cn/j2se/ServletException.html">ServletException</a>, <a target="_blank" href="http://www.myexception.cn/j2se/IOException.html">IOException</a> {
System.out.println("SelectAllServer");
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
JDBCConnection jb = new JDBCConnection();
int start = Integer.parseInt(req.getParameter("start"));
int limit = Integer.parseInt(req.getParameter("limit"));
AVInfo info;
String sql = "select count(*) from avactor";
ResultSet rs = jb.doQuery(sql);
int total = 0;
try {
if (rs.next()) {
total = rs.getInt(1);
}
} catch (<a target="_blank" href="http://www.myexception.cn/java-other/SQLException.html">SQLException</a> e) {
e.printStackTrace();
}
int arrayIndex = 0;
int totalSelect = start + limit;
if (totalSelect > total) {// 简单解决数组问题
if (100 < totalSelect && totalSelect < 1000) {
arrayIndex = total % 100;
}
if (10 < totalSelect && totalSelect < 100) {
arrayIndex = total % 10;
}
} else {
arrayIndex = 10;
}
if (total < 10) {
arrayIndex = total;
}
String jieguo = "";
jieguo = "{totalProperty:" + total + ",";
jieguo += ("root:");
sql = "select * from avactor limit ?,?";
Object[] s = { start, limit };
rs = null;
rs = jb.doQuery(sql, s);
Object[] ob = new Object[arrayIndex];
int index = 0;
try {
while (rs.next()) {
info = new AVInfo();
info.setId(rs.getInt("id"));
info.setBWH(rs.getString("BWH"));
info.setAge(rs.getInt("age"));
info.setDescription(rs.getString("description"));
info.sethAndw(rs.getString("hAndw"));
info.setHeadImage(rs.getString("headImage"));
info.setName(rs.getString("name"));
ob[index] = info;
index++;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
JSONArray json = JSONArray.fromObject(ob);
jieguo += json;
jieguo += "}";
PrintWriter pw = resp.getWriter();
System.out.println(jieguo);
pw.write(jieguo);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
} //源代码片段来自云代码http://yuncode.net