EasyUI CRUD应用Java改造版本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EasyUI CRUD应用Java改造版本相关的知识,希望对你有一定的参考价值。
EasyUI的CRUD应用采用的是php实现后台功能,将其改造为java版本实现数据表格的增删改功能。
EasyUI的教程及代码https://www.jeasyui.com/tutorial/app/crud.php
源代码结构:
1、建立数据库表,将users.sql导入mysql即可
2、修改index.html,将index.html中url的get_user.php替换为自己写的jsp代码
<table id="dg" title="My Users" class="easyui-datagrid" style="width:700px;height:250px" url="get_users.php" toolbar="#toolbar" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true">
3、get_user.jsp,其中page和rows是翻页的参数,offset是用于分页查询使用的参数,需要返回总的记录数和数据库数据,采用json数据返回
<%@ page import="org.json.*" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="java.sql.ResultSetMetaData" %> <%@ page import="java.sql.Statement" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% //获取页数、显示记录参数 int page1=Integer.parseInt(request.getParameter("page")); int rows1=Integer.parseInt(request.getParameter("rows")); int offset=(page1-1)*rows1; int total=0; String DB_driver="com.mysql.jdbc.Driver"; String u="jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=GBK"; Class.forName(DB_driver); java.sql.Connection con = java.sql.DriverManager.getConnection(u); java.sql.Statement stmt =con.createStatement(); String sql="select count(*) total from users"; java.sql.ResultSet rs = stmt.executeQuery(sql); if(rs.next()) total=rs.getInt("total"); JSONArray array = new JSONArray(); JSONObject jsonObj2 = new JSONObject(); sql="select * from users limit "+offset+","+rows1; rs = stmt.executeQuery(sql); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while (rs.next()) { JSONObject jsonObj = new JSONObject(); for(int i=1;i<columnCount+1;i++) { String columnName =metaData.getColumnLabel(i); String value = rs.getString(columnName); jsonObj.put(columnName, value); } array.put(jsonObj); } jsonObj2.put("total",total); jsonObj2.put("rows",array); rs.close(); stmt.close(); con.close(); out.write(jsonObj2.toString()); %>
4、添加用户
<%@ page import="org.json.*" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.ResultSet" %> <%@ page import="java.sql.ResultSetMetaData" %> <%@ page import="java.sql.Statement" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% //获取user参数 String firstname= request.getParameter("firstname"); String lastname= request.getParameter("lastname"); String phone= request.getParameter("phone"); String email= request.getParameter("email"); JSONObject result = new JSONObject(); int id=0; String DB_driver="com.mysql.jdbc.Driver"; String u="jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=GBK"; Class.forName(DB_driver); java.sql.Connection con = java.sql.DriverManager.getConnection(u); java.sql.Statement stmt =con.createStatement(); String sql="insert into users(firstname,lastname,phone,email) values(‘"+firstname+"‘,‘"+lastname+"‘,‘"+phone+"‘,‘"+email+"‘)"; int flag=stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); java.sql.ResultSet rs = stmt.getGeneratedKeys(); if (rs.next()) { id = rs.getInt(1); } sql="select * from users where id="+id; rs=stmt.executeQuery(sql); if(rs.next()){ result.put("id",rs.getInt("id")); result.put("firstname",rs.getString("firstname")); result.put("lastname",rs.getString("lastname")); result.put("phone",rs.getString("phone")); result.put("email",rs.getString("email")); } else{ result.put("errorMsg","insert data failed"); } stmt.close(); con.close(); out.write(result.toString()); %>
5、删除用户
<%@ page import="org.json.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% //获取user参数 String firstname= request.getParameter("firstname"); String lastname= request.getParameter("lastname"); String phone= request.getParameter("phone"); String email= request.getParameter("email"); int id=Integer.parseInt(request.getParameter("id")); JSONObject result = new JSONObject(); String DB_driver="com.mysql.jdbc.Driver"; String u="jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=GBK"; Class.forName(DB_driver); java.sql.Connection con = java.sql.DriverManager.getConnection(u); java.sql.Statement stmt =con.createStatement(); String sql="delete from users where id=‘"+id+"‘"; int flag=stmt.executeUpdate(sql); if(flag>0){ result.put("success","true"); } else{ result.put("errorMsg","this is wrong"); } stmt.close(); con.close(); out.write(result.toString()); %>
6、最终效果
修改用户
删除用户
以上是关于EasyUI CRUD应用Java改造版本的主要内容,如果未能解决你的问题,请参考以下文章
雷林鹏分享:jQuery EasyUI 应用 - 创建 CRUD 数据网格(DataGrid)
雷林鹏分享:jQuery EasyUI 应用 - 创建展开行明细编辑表单的 CRUD 应用