package wk.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import wk.model.UserBean;
import wk.util.DBTool;

public class UserJsonServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 //请求发送都是utf-8 
		 request.setCharacterEncoding("UTF-8");
         response.setCharacterEncoding("UTF-8");
         
		List<UserBean> list = new ArrayList<UserBean>();
		DBTool dbt = new DBTool();
		PreparedStatement pstmt = null;
		Connection conn = null;
		ResultSet rs = null;
		String json = null;
		try {
			conn = dbt.getConnection();
			String sql = "SELECT id,name,sex,age,tell,address FROM testexcel";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				UserBean ub = new UserBean();
				ub.setId(rs.getInt("id"));
				ub.setName(rs.getString("name"));
				ub.setSex(rs.getString("sex"));
				ub.setAge(rs.getInt("age"));
				ub.setTell(rs.getInt("tell"));
				ub.setAddress(rs.getString("address"));
				list.add(ub);
			}

			json = JSON.toJSONString(list);
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(json);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
最近下载更多
601601lmy  LV5 2023年10月20日
lilily1119  LV4 2023年4月29日
lcy123ww  LV5 2022年11月22日
zhangbo2020  LV6 2022年8月22日
liu2022  LV14 2022年7月31日
一直都会顺利的小吴  LV5 2022年2月16日
雪人飞飞  LV2 2022年1月3日
ewan007  LV29 2021年12月26日
地方撒地方的  LV2 2021年12月18日
sl0018  LV13 2021年9月23日
最近浏览更多
601601lmy  LV5 2023年10月20日
可是不知道么  LV23 2023年5月6日
lilily1119  LV4 2023年4月29日
xiao小果  LV12 2023年3月23日
hesu2020  LV1 2023年3月20日
夜起星河  LV8 2023年3月9日
微信网友_6145740972576768  LV2 2022年12月21日
lcy123ww  LV5 2022年11月22日
zhangbo2020  LV6 2022年8月22日
是你爸爸啊100  LV5 2022年8月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友