用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

json转换

2015-05-17 作者: java源代码大全举报

[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
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...