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自动访问数据库的更多相关文章

  1. 单页js文件访问数据库

    最原始的编程方式,业务逻辑混杂在html中 <%@ page language="java" import="java.util.*" pageEncod ...

  2. js访问数据库

    一.js访问数据库的一般步骤: 1. 创建一个到数据库的 ADO 连接 conn = new ActiveXObject("ADODB.Connection"); 2. 打开数据库 ...

  3. 采用异步来实现重新连接服务器或者重新启动服务 C#中类的属性的获取 SignalR2简易数据看板演示 C#动态调用泛型类、泛型方法 asp .net core Get raw request. 从壹开始前后端分离[.NetCore 不定期更新] 38 ║自动初始化数据库

    采用异步来实现重新连接服务器或者重新启动服务 开启异步监听,不会导致主线程的堵塞,在服务异常断开后一直检测重新连接服务,成功连接服务后通知各个注册的客户端! #region 检测断线并重连OPC服务 ...

  4. MVC Code First 自动生成数据库

    1.新建一个MVC项目

  5. (转)发布Silverlight+WCF程序到IIS后,客户端访问数据库失败的解决方案

    转自url:http://greatverve.cnblogs.com/archive/2011/11/30/silverlight-wcf-pub.html 我们在编写Silverlight程序时, ...

  6. EntityFramework系列:SQLite.CodeFirst自动生成数据库

    http://www.cnblogs.com/easygame/p/4447457.html 在Code First模式下使用SQLite一直存在不能自动生成数据库的问题,使用SQL Server C ...

  7. Entity FrameWork 中使用Expression<Func<T,true>>访问数据库性能优化

    问题的本质是:扩展的Where方法有四个参数重载.传进去Func<T,true>那么返回值是IEnumable的接口类型的集合,如果是Expression<Func<T,tru ...

  8. Spring实战6:利用Spring和JDBC访问数据库

    主要内容 定义Spring的数据访问支持 配置数据库资源 使用Spring提供的JDBC模板 写在前面:经过上一篇文章的学习,我们掌握了如何写web应用的控制器层,不过由于只定义了SpitterRep ...

  9. oracle 事务简介,锁的概念,java访问数据库注意事项

    java链接oracle和连接其他数据库一样有两种方式:1 桥接 jdbc-obdc2 jbdc insert语句一次插入大量数据 insert into table (列1,列2,列3) selec ...

随机推荐

  1. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  2. windowsAPI创建句柄失败的返回值

    创建句柄的api返回值 INVALID_HANDLE_VALUE CreateFile CreateNamedPipe CreateToolhelp32Snapshot FilterConnectCo ...

  3. C#数字日期转成中文日期

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  4. PG SQL funcation

    create extension IF NOT EXISTS "uuid-ossp" ; --select uuid_generate_v4(); --select current ...

  5. leetcode算法题(4)

    问题描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 我的解答: package Simple; public class RoamnInt { public static ...

  6. J.U.C之重入锁:ReentrantLock

    此篇博客所有源码均来自JDK 1.8 ReentrantLock,可重入锁,是一种递归无阻塞的同步机制.它可以等同于synchronized的使用,但是ReentrantLock提供了比synchro ...

  7. TortoiseSVN-1.7.12.24070-x64-svn-1.7.9安装包和汉化包

    链接:https://pan.baidu.com/s/1NbrQW44N_kTh7VN0Fz0zVA 提取码:nhd9 先安装TortoiseSVN-1.7.12.24070-x64-svn-1.7. ...

  8. 树莓派手动设置静态IP和DNS方法

    在使用树莓派的过程中,往往需要手动设置一个静态的IP地址,一来可以防止DHCP自动分配的IP变动,二来可提高树莓派的网络连接速度.查看官方文档 man dhcpcd.conf可知,需要配置静态IP的话 ...

  9. stm32 SD卡

    容量等级 SD容量有8MB.16MB.32MB.64MB.128MB.256MB.512MB.1GB.2GB SDHC容量有2GB .4GB.8GB.16GB.32GB SDXC容量有32GB.48G ...

  10. Linux下知道一个命令却不知道哪个包提供(解决)

    [root@localhost ~]# yum -y install jstack (1/2): epel/x86_64/primary_db | 6.8 MB 00:00:16 (2/2): epe ...