ajax异步加载查询数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数据</title>
<link rel="stylesheet" href="static/css/style.css" type="text/css">
<!-- jQuery-easyUI start -->
<link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/themes/color.css">
<link rel="stylesheet" type="text/css" href="https://www.jeasyui.com/easyui/demo/demo.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="https://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
<!-- jQuery-easyUI end -->
<script type="text/javascript">
document.createElement("section");
document.createElement("article");
document.createElement("footer");
document.createElement("header");
document.createElement("hgroup");
document.createElement("nav");
document.createElement("menu");
</script>
<script type="text/javascript">
function Save() {
document.getElementById("PageOfficeCtrl1").WebSave();
}
</script>
</head>
<body>
<header>
<div class="w12 header">
<a class="db logo fl"><img src="static/images/logo.jpg"
width="" height="" alt="" />
</a>
<div class="fr logofr">
<a href="#" class="blk">返回首页</a> |<a href="#" class="blk">客服中心</a><br>
如注册遇到问题请拨打:<strong style="font-size:14px;">--</strong>
</div>
</div>
</header>
<div class="head_border"></div>
<section class="w12 login"> <em class="fr">当前用户:张三 </em> </section>
<section class="main w12">
<div class="title">
<a class="title1 db fl">审核报告</a><a class="title2 db fl">审核合同</a>
</div>
<div class="fr tit2">
<span class="arr"></span>
</div>
</section>
<br /><br /> <div style="text-align:content;margin-left: 300px;">
<table id="dg" title="北京某某科技有限公司" class="easyui-datagrid" style="width:700px;height:250px;,margin: auto;"
url="get_users.php" toolbar="#toolbar" pagination="true" rownumbers="true" fitColumns="true" singleSelect="true">
<thead>
<tr id="insertSpareEpt">
<th field="firstname" width="">单位编号</th>
<th field="lastname" width="">单位名称</th>
<th field="phone" width="">单位性质</th>
<th field="email" width="">单位地址</th>
</tr>
</thead>
</table>
</div> <div id="toolbar">
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">添加</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">修改</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除</a>
</div>
<div id="divTab"></div>
<!-- 弹窗 -->
<div id="dlg" class="easyui-dialog" style="width:400px" data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons'">
<form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
<h3>单位信息</h3>
<div style="margin-bottom:10px">
<input name="firstname" class="easyui-textbox" required="true" label="单位编号:" style="width:100%">
</div>
<div style="margin-bottom:10px">
<input name="lastname" class="easyui-textbox" required="true" label="单位名称:" style="width:100%">
</div>
<div style="margin-bottom:10px">
<input name="phone" class="easyui-textbox" required="true" label="单位性质:" style="width:100%">
</div>
<div style="margin-bottom:10px">
<input name="email" class="easyui-textbox" required="true" validType="email" label="单位地址:" style="width:100%">
</div>
</form>
</div>
<div id="dlg-buttons">
<a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">添加</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
</div> <script type="text/javascript">
var url;
function newUser(){
$('#dlg').dialog('open').dialog('center').dialog('setTitle','New User');
$('#fm').form('clear');
url = 'save_user.php';
}
function editUser(){
var row = $('#dg').datagrid('getSelected');
if (row){
$('#dlg').dialog('open').dialog('center').dialog('setTitle','Edit User');
$('#fm').form('load',row);
url = 'update_user.php?id='+row.id;
}
}
function saveUser1(){
$('#fm').form('submit',{
url: saveInfo,
onSubmit: function(){
return $(this).form('validate');
},
success: function(result){
var result = eval('('+result+')');
if (result.errorMsg){
$.messager.show({
title: 'Error',
msg: result.errorMsg
});
} else {
$('#dlg').dialog('close'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
}
}
});
} /* 保存 */
function saveUser(){
var info;
var firstname = $("input[name='firstname']").val();
var lastname = $("input[name='lastname']").val();
var phone = $("input[name='phone']").val();
var email = $("input[name='email']").val();
$.ajax({
type : 'get',
url : 'saveInfo',
data:{
"firstname":firstname,
"lastname":lastname,
"phone":phone,
"email":email
},
cache : false,
async : false,
dataType : 'json',
success : function(data){
$("#dg").datagrid("loadData",data);// 数据加载jQuery easyUI表格中 /* var tabStr = "";
$.each(data, function(index,value){
var item = data[index];
var itemFirstname = item.firstname;
var itemLastname = item.lastname;
var itemPhone = item.phone;
var itemEmail = item.email;
var itemStr = "<table border='1' cellpadding='0' cellspacing='0'><tr>"+
"<td name='firstname' width=50>"+itemFirstname+"</td>"+
"<td name='lastname' width=50>"+itemLastname+"</td>"+
"<td name='phone' width=50>"+itemPhone+"</td>"+
"<td name='email' width=50>"+itemEmail+"</td>"+
"</tr></table>";
tabStr += itemStr;
})
document.getElementById("divTab").innerHTML = tabStr; */ } , error:function(data){
alert("加载数据失败!"+data);
}
});
// 关闭添加窗口
$('#dlg').dialog('close');
} function destroyUser(){
var row = $('#dg').datagrid('getSelected');
if (row){
$.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){
if (r){
$.post('destroy_user.php',{id:row.id},function(result){
if (result.success){
$('#dg').datagrid('reload'); // reload the user data
} else {
$.messager.show({ // show error message
title: 'Error',
msg: result.errorMsg
});
}
},'json');
}
});
}
}
</script>
</body>
</html> package com.wyebd.controller; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.wyebd.bean.Contract;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument; @RestController
public class ReadDataController { private static Logger log = Logger.getLogger(ReadOnlyController.class);
private final String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
private static final String USER = "root";
private static final String PASS = "root";
private static Connection con = null;
private static PreparedStatement ps = null;
private static Statement stmt = null;
private static ResultSet rs = null; /**
* 跳转至查询页面
* @param request
* @param response
* @param map
* @return
*/
@RequestMapping(value = "/readData", method = RequestMethod.GET)
public ModelAndView readData() {
ModelAndView mv = new ModelAndView("readData");
return mv;
} /**
* 保存
* @param request
* @param response
* @param map
* @return
*/
@RequestMapping("saveInfo")
public String saveInfo(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
List<Contract> list = new ArrayList<Contract>();
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String json = null;
int id = ;
try {
getConnection();
ps = con.prepareStatement("insert into test values (?,?,?,?,?)");
ps.setInt(, id);
ps.setString(, firstname);
ps.setString(, lastname);
ps.setString(,phone);
ps.setString(, email);
ps.executeUpdate();
log.info("添加成功");
rs = stmt.executeQuery("select * from test");
while(rs.next()){
Contract con = new Contract();
con.setFirstname(rs.getString("firstname"));
con.setLastname(rs.getString("lastname"));
con.setPhone(rs.getString("phone"));
con.setEmail(rs.getString("email"));
list.add(con);
}
// list转为json传入前端
json = JSON.toJSONString(list);
log.info(json);
// 取得流向JSP传递数据
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println(json);
response.reset();
} catch (Exception e) {
log.info("SQLException: " + e.getMessage());
e.printStackTrace();
} finally {
try {
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return json;
} /**
* 连接数据库
* @throws ClassNotFoundException
* @throws SQLException
*/
public void getConnection() throws ClassNotFoundException, SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = con.createStatement();
} catch (Exception e) {
System.out.println("SQLException: " + e.getMessage());
e.printStackTrace();
}
} }
ajax异步加载查询数据库的更多相关文章
- 淘宝购物车页面 智能搜索框Ajax异步加载数据
如果有朋友对本篇文章的一些知识点不了解的话,可以先阅读此篇文章.在这篇文章中,我大概介绍了一下构建淘宝购物车页面需要的基础知识. 这篇文章主要探讨的是智能搜索框Ajax异步加载数据.jQuery的社区 ...
- Scrapy爬虫框架教程(四)-- 抓取AJAX异步加载网页
欢迎关注博主主页,学习python视频资源,还有大量免费python经典文章 sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction ...
- jQuery的AJax异步加载
主要用到load()方法以及getScript()方法,具体以一个例子说明: 在现有html文件中加载一个拟好的片段,以及在片段加载完成之前阻止用户进一步操作的弹出框. 首先是现有html代码,无任何 ...
- ajax异步加载问题
使用ajax异步加载数据,在之后需要用到这个数据时,应该将之后的js一并写入ajax函数中,否则后面的js不能找到动态拼接的dom节点. 或者将其封装成方法,在ajax动态加载数据的最后调用该方法.
- Ajax 异步加载
AJAX (Asynchronous JavaScript and XML,异步的 JavaScript 和 XML).它不是新的编程语言,而是一种使用现有标准的新方法,是在不重新加载整个页面的情况下 ...
- 使用Ajax异步加载页面时,怎样调试该页面的Js
前言-本人不是干前端的,所以有的名词不专业 在前端中,有时候会遇到这样的框架,http://172.17.11.151:8060/frontend/backend.html#1.html (通过解析U ...
- ajax异步加载回跳定位
1)首先,问题是这样的:page1在一个滚动到页面底部加载更新的函数(功能),当页面滚动了第二页(或更多页)时,点击链接跳转到了其他页面(page2),在page2有返回按钮,当从page2返回pag ...
- AJAX异步加载
AJAX含义: 即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX可以跨 ...
- ajax 异步加载显示等待效果
css: #loading { width:170px; height:25px; border:3px solid #C3DAF9; position:absolute; top:300px; le ...
随机推荐
- 实现远程线程DLL注入
### 32位:远程线程注入 远程线程注入是最常用的一种注入技术,该技术利用的核心API是 `CreateRemoteThread()` 这个API可以运行远程线程,其次通过创建的线程调用 `Load ...
- sql server truncate语句
truncate语句 --truncate table '表名' --这样就利用SQL语句清空了该数据表,而不保留日志
- QuickSort(快排)的JAVA实现
QuickSort的JAVA实现 这是一篇算法课程的复习笔记 用JAVA对快排又实现了一遍. 先实现的是那个easy版的,每次选的排序轴都是数组的最后一个: package com.algorithm ...
- 禁止缩放meta标签
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale= ...
- 渗透神器CobaltStrike 3.1.2 去后门破解版 & Windows版TeamServer【转】
转自我八师傅博客 CS简介 Cobalt Strike(简称CS)是全球黑客公认一款非常优秀的渗透测试神器,以metasploit为基础的GUI的框架式渗透工具,集成了传统远控功能(远程桌面VNC.键 ...
- 测试Random类nextInt()方法连续两次结果一样的概率
public static void main(String[] args) { int count = 0; int a = 0; Random r = new Random(); while (t ...
- Linux课程学习 第二课
工欲善其事,必先利其器 虚拟机安装(链接中有详细的操作方法,这里就不再详细说明了,但有注意事项,会在下文中截图标注) https://www.linuxprobe.com/ 注:为了避免是权限问题导 ...
- C语言字符串操作小结
1)字符串操作strcpy(p, p1) 复制字符串strncpy(p, p1, n) 复制指定长度字符串strcat(p, p1) 附加字符串strncat(p, p1, n) 附加指定长度字符串s ...
- MP4 ISO基础媒体文件格式 摘要 1
目录 Object-structured File Organization 1 File Type Box (ftyp) Box Structures File Structure and gene ...
- 多线程 - 线程通信 suspend-resume wait-notify park-unpark 伪唤醒
线程通信(如 线程执行先后顺序,获取某个线程执行的结果等)有多种方式: 文件共享 线程1 --写入--> 文件 < --读取-- 线程2 网络共享 变量共享 线程1 --写入--> ...