1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| package com.xiaoguan.request;
import com.alibaba.fastjson.JSON; import com.xiaoguan.bean.Information; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.PrintWriter; import java.sql.*; import java.util.ArrayList; import java.util.List;
@WebServlet("/ajax5") public class ajax5Servlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); String jsonstr=""; Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/xiaoguan","root","68963120g"); String sql="select 登录名,登录密码,真实姓名 from login "; ps=conn.prepareStatement(sql); rs=ps.executeQuery();
List<Information> infoList=new ArrayList<>(); while (rs.next()) { String 登录名 = rs.getString("登录名"); String 登录密码 = rs.getString("登录密码"); String 真实姓名 = rs.getString("真实姓名"); Information info=new Information(登录名,登录密码,真实姓名); infoList.add(info); } jsonstr = JSON.toJSONString(infoList); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (rs!=null) { try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (ps!=null) { try { ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (rs!=null) { try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } }
out.print(jsonstr); } }
|