做作业工程中遇到一些问题,特此记录一下
解决的问题:使用EasyUI框架搭建简单学生管理系统(数据库增删改查)操作时配合JSP,不知道如何把从数据库获得的数据封装成JSON格式并传回前端JSP并进行展示
解决工具:Servlet、MVC设计模式
1.数据封装
//连接数据库 Connection connection=null; String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎 String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=tjl";//数据源 !!!注意若出现加载或者连接数据库失败一般是这里出现问题 String Name="sa"; String Pwd="19990713"; ResultSet rs=null; //定义JSON数组,用于保存从数据库查出来的数据 JSONArray array = new JSONArray(); try{ Class.forName(driverName); connection=DriverManager.getConnection(dbURL,Name,Pwd); System.out.println("连接数据库成功"); }catch(Exception e){ e.printStackTrace(); System.out.println("连接失败"); } Statement stmt = null; try { stmt = connection.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*//增删改操作 String sql_1="insert into student values(\'990403\',\'佩奇\',\'男\',\'12\',\'1\',\'2101\')"; int count_1 = 0; try { count_1 = stmt.executeUpdate(sql_1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }//返回值表示增删改几条数据 //处理结果 if(count_1>0){ System.out.println("更新成功!"); }*/ //查询操作 String sql_2="select * from student"; try { rs = stmt.executeQuery(sql_2); // 获取列数 ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while (rs.next()) { JSONObject jsonObj = new JSONObject(); // 遍历每一列 for (int i = 1; i <= columnCount; i++) { String columnName =metaData.getColumnLabel(i); String value = rs.getString(columnName); jsonObj.put(columnName, value); } array.put(jsonObj); } } catch (SQLException e1) { e1.printStackTrace(); }
2.Servlet基础知识
3.在Ecplise内创建Servlet
在您所创建的WEB项目的src目录右键新建,如图,选择Servlet,如果有版本选项,选择3.0版本,系统会自动创建映射和基本都代码格式
4.完整代码
package com.jwglxt.data; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; import org.json.JSONObject; @WebServlet("/getStudent") public class getStudent extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置编码格式 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/json; charset=utf-8"); //连接数据库 Connection connection=null; String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎 String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=tjl";//数据源 !!!注意若出现加载或者连接数据库失败一般是这里出现问题 String Name="sa"; String Pwd="19990713"; ResultSet rs=null; //定义JSON数组,用于保存从数据库查出来的数据 JSONArray array = new JSONArray(); try{ Class.forName(driverName); connection=DriverManager.getConnection(dbURL,Name,Pwd); System.out.println("连接数据库成功"); }catch(Exception e){ e.printStackTrace(); System.out.println("连接失败"); } Statement stmt = null; try { stmt = connection.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*//增删改操作 String sql_1="insert into student values(\'990403\',\'佩奇\',\'男\',\'12\',\'1\',\'2101\')"; int count_1 = 0; try { count_1 = stmt.executeUpdate(sql_1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }//返回值表示增删改几条数据 //处理结果 if(count_1>0){ System.out.println("更新成功!"); }*/ //查询操作 String sql_2="select * from student"; try { rs = stmt.executeQuery(sql_2); // 获取列数 ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while (rs.next()) { JSONObject jsonObj = new JSONObject(); // 遍历每一列 for (int i = 1; i <= columnCount; i++) { String columnName =metaData.getColumnLabel(i); String value = rs.getString(columnName); jsonObj.put(columnName, value); } array.put(jsonObj); } } catch (SQLException e1) { e1.printStackTrace(); } //关闭 try { if(rs!=null) rs.close(); stmt.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } PrintWriter out = response.getWriter(); out.println(array); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
5.EasyUI引用数据
参考文章:https://blog.csdn.net/qq_38313548/article/details/85966603