js自动访问数据库
js自动访问数据库
maven依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
Servlet
用的三层架构,service,dao层就不写了,用的是C3P0连接池,mysql为数据库
最后返回的是一个json字符串
//测试前端是否能访问
//System.out.println("getPlanePosition方法被调用了...");
PlaneService planeService = new PlaneServiceImpl();
PlanePosition planePosition = planeService.findPosition();
Gson gson = new Gson();
String jsonStr = gson.toJson(planePosition, PlanePosition.class);
//设置响应头为json对象
response.setContentType("application/json;charset=utf-8");
//返回一个json对象
response.getWriter().print(jsonStr);
//是否能输出正确的值
//System.out.println(jsonStr);
JSutils
之后需要用到的小工具
/**
* 获取部署的项目地址
* @returns {string}
*/
function contextPath(){
// var curWwwPath = window.document.location.href;
var pathName = window.document.location.pathname;
// var pos = curWwwPath.indexOf(pathName);
// var localhostPaht = curWwwPath.substring(0,pos);
// var projectName = pathName.substring(0,pathName.substr(1).indexOf('/')+1);
// return (localhostPaht + projectName);
var number = pathName.indexOf("/", 1);
return pathName.substring(0,number);
}
/**
* 生成XMLHttpRequest
* @returns {XMLHttpRequest}
*/
function ajaxFunction() {
var xmlHttp;
try { // Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {// Internet Explorer
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
return xmlHttp;
}
html中js代码
模拟get方法
//全局变量
var yPosition;
function get() {
//1. 创建xmlhttprequest 对象
var request = ajaxFunction();
//2. 发送请求 用false是因为需要同步(true为异步)
request.open("GET", "PlaneServlet?method=getPlanePosition", false);
//3. 获取响应数据 注册监听的意思。 一会准备的状态发生了改变,那么就执行 = 号右边的方法
request.onreadystatechange = function () {
//前半段表示 已经能够正常处理。 再判断状态码是否是200
if (request.readyState == 4 && request.status == 200) {
//弹出响应的信息,测试用
// alert(request.responseText);
//转为json对象
var obj = JSON.parse(request.responseText);
//把服务器响应的json对象赋值给yPosition
yPosition = obj.yPosition;
// alert(yPosition);
}
};
request.send();
}
设置每2秒刷新一次
var myVar = setInterval(function () {
ChangePosition();
}, 2000);
js自动访问数据库的更多相关文章
- 单页js文件访问数据库
最原始的编程方式,业务逻辑混杂在html中 <%@ page language="java" import="java.util.*" pageEncod ...
- js访问数据库
一.js访问数据库的一般步骤: 1. 创建一个到数据库的 ADO 连接 conn = new ActiveXObject("ADODB.Connection"); 2. 打开数据库 ...
- 采用异步来实现重新连接服务器或者重新启动服务 C#中类的属性的获取 SignalR2简易数据看板演示 C#动态调用泛型类、泛型方法 asp .net core Get raw request. 从壹开始前后端分离[.NetCore 不定期更新] 38 ║自动初始化数据库
采用异步来实现重新连接服务器或者重新启动服务 开启异步监听,不会导致主线程的堵塞,在服务异常断开后一直检测重新连接服务,成功连接服务后通知各个注册的客户端! #region 检测断线并重连OPC服务 ...
- MVC Code First 自动生成数据库
1.新建一个MVC项目
- (转)发布Silverlight+WCF程序到IIS后,客户端访问数据库失败的解决方案
转自url:http://greatverve.cnblogs.com/archive/2011/11/30/silverlight-wcf-pub.html 我们在编写Silverlight程序时, ...
- EntityFramework系列:SQLite.CodeFirst自动生成数据库
http://www.cnblogs.com/easygame/p/4447457.html 在Code First模式下使用SQLite一直存在不能自动生成数据库的问题,使用SQL Server C ...
- Entity FrameWork 中使用Expression<Func<T,true>>访问数据库性能优化
问题的本质是:扩展的Where方法有四个参数重载.传进去Func<T,true>那么返回值是IEnumable的接口类型的集合,如果是Expression<Func<T,tru ...
- Spring实战6:利用Spring和JDBC访问数据库
主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...
- oracle 事务简介,锁的概念,java访问数据库注意事项
java链接oracle和连接其他数据库一样有两种方式:1 桥接 jdbc-obdc2 jbdc insert语句一次插入大量数据 insert into table (列1,列2,列3) selec ...
随机推荐
- 音视频入门-08-RGB&YUV
* 音视频入门文章目录 * YUV & RGB 相互转换公式 YCbCr 的 Y 与 YUV 中的 Y 含义一致,Cb 和 Cr 与 UV 同样都指色彩,Cb 指蓝色色度,Cr 指红色色度,在 ...
- 翻译-在10行代码之内创建容器化的.net core应用
本文翻译自Hans Kilian的文章 Creating a containerized .NET core application in less than 10 lines of code htt ...
- Pytorch 1.0升级到Pytorch 1.1.0
Pytorch 1.0Pytorch 1.0于2018-12-8发布,详见https://github.com/pytorch/pytorch/releases/tag/v1.0.0 主要更新JIT全 ...
- thinkphp中 select() 和find() 方法的区别
$about=M('document'); $abouts=$about->where('id=2')->select(); $abouts2=$about->where('id=2 ...
- 移动端隐藏滚动条,css方法
小白第一次发文记录自己遇到的问题. 关于隐藏移动端滚动条方法很多,这里只说本人用到的. 在PC端隐藏html右侧默认滚动条 html { /*隐藏滚动条,当IE下溢出,仍然可以滚动*/ -ms-ove ...
- js入门之字符串常用的方法
一. 概念理解基本包装类型 1. 基本包装类型 三种基本包装类型 String var s = new String('123dddd'); Number Boolean 简单类型没有方法和属性 之所 ...
- js数组实现上移下移
up(index) { if(index === 0) { return } //在上一项插入该项 this.list.splice(index - 1, 0, (this.list[index])) ...
- springboot 常见的启动器
<!--pringBoot提供了一个名为spring-boot-starter-parent的工程, 里面已经对各种常用依赖(并非全部)的版本进行了管理 我们的项目需要以这个项目为父工程,这样我 ...
- How to : SAP PI Cache Refresh
Requirement : Identify various tools/resources available to perform SAP PI Cache refresh . Please no ...
- Python排序算法(六)——归并排序(MERGE-SORT)
有趣的事,Python永远不会缺席! 如需转发,请注明出处:小婷儿的python https://www.cnblogs.com/xxtalhr/p/10800699.html 一.归并排序(MERG ...