Ajax之数据连接信息捕获

 
connDB.properties:
DB_CLASS_NAME=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://127.0.0.1:3306/db_database16?user=root&password=111&useUnicode=true
package com.caiduping.core;

//导入java.io.InputStream类
import java.io.InputStream;
//导入java.sql包中的所有类
import java.sql.*;
//导入java.util.Properties类
import java.util.Properties;

public class ConnDB {
    // 声明Connection对象的实例
    public Connection conn=null;
    // 声明Statement对象的实例
    public Statement stmt=null;
    // 声明ResultSet对象的实例
    public ResultSet rs=null;
    // 指定资源文件保存的位置
    public static String propFileName="connDB.properties";
    // 创建并实例化Properties对象的实例
    public static Properties prop=new Properties();
    //定义保存数据库驱动的变量
    public static String dbClassName="com.mysql.jdbc.Driver";
    private static String dbUrl = "jdbc:mysql://127.0.0.1:3306/db_database16?user=root&password=111&useUnicode=true";
    /*
     * 构造方法
     * 
     * */
    public ConnDB(){
        try{
            //将Properties文件读取到InputStream对象中
            InputStream in=getClass().getResourceAsStream(propFileName);
            // 通过输入流对象加载Properties文件
            prop.load(in);
            // 获取数据库驱动
            dbClassName=prop.getProperty("DB_CLASS_NAME");
            //获取URL
            dbUrl=prop.getProperty("DB_URL","dbUrl");
        }catch(Exception e){
            // 输出异常信息
            e.printStackTrace();
        }
    }
    /*
     * 建立数据连接
     * 
     * */
    public static Connection getConnection(){
        Connection conn=null;
        //连接数据库时可能发生异常因此需要捕捉该异常
        try{
            //装载数据库驱动
            Class.forName(dbClassName).newInstance();
            //建立与数据库URL中定义的数据库的连接
            conn=DriverManager.getConnection(dbUrl);
        }catch(Exception ee){
        ee.printStackTrace();    
        }
        if (conn == null) {
            //在控制台上输出提示信息
            System.err.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.

链接类型:"+dbClassName+"
链接位置:"+dbUrl);        
        }
        return conn;    
    }
    /*
     * 执行查询语句
     * 
     * */
    public ResultSet executeQuery(String sql) {
        try {
            // 调用getConnection()方法构造Connection对象的一个实例conn
            conn = getConnection(); 
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            //执行SQL语句,并返回一个ResultSet对象rs
            rs = stmt.executeQuery(sql);            
        } catch (SQLException ex) {
            // 输出异常信息
            System.err.println(ex.getMessage()); 
        }
        // 返回结果集对象
        return rs; 
    }
    /*
     * 执行更新操作
     * 
     * */
    public int executeUpdate(String sql) {
        // 定义保存返回值的变量
        int result = 0; 
        try { 
            // 调用getConnection()方法构造Connection对象的一个实例conn
            conn = getConnection(); 
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
            // 执行更新操作
            result = stmt.executeUpdate(sql); 
        } catch (SQLException ex) {
            // 将保存返回值的变量赋值为0
            result = 0; 
        }
        // 返回保存返回值的变量
        return result; 
    }
    /*
     * 关闭数据库的连接
     * 
     * */
    public void close() {
        try { 
            // 当ResultSet对象的实例rs不为空时
            if (rs != null) { 
                // 关闭ResultSet对象
                rs.close(); 
            }
            // 当Statement对象的实例stmt不为空时
            if (stmt != null) { 
                // 关闭Statement对象
                stmt.close(); 
            }
            // 当Connection对象的实例conn不为空时
            if (conn != null) { 
                // 关闭Connection对象
                conn.close(); 
            }
        } catch (Exception e) {
            // 输出异常信息
            e.printStackTrace(System.err); 
        }
    }
}
ConnDB
// 定义一个全局变量net
var net = new Object(); 
// 编写构造函数
net.AjaxRequest = function(url, onload, onerror, method, params) {
    this.req = null;
    this.onload = onload;
    this.onerror = (onerror) ? onerror : this.defaultError;
    this.loadDate(url, method, params);
}
// 编写用于初始化XMLHttpRequest对象并指定处理函数,最后发送HTTP请求的方法
net.AjaxRequest.prototype.loadDate = function(url, method, params) {
    if (!method) {
        method = "GET";
    }
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        this.req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (this.req) {
        try {
            var loader = this;
            this.req.onreadystatechange = function() {
                net.AjaxRequest.onReadyState.call(loader);
            }
            // 建立对服务器的调用
            this.req.open(method, url, true);
            // 如果提交方式为POST
            if (method == "POST") {
                this.req.setRequestHeader("Content-Type",
                        // 设置请求头
                        "application/x-www-form-urlencoded"); 
            }
            // 发送请求
            this.req.send(params); 
        } catch (err) {
            this.onerror.call(this);
        }
    }
}

// 重构回调函数
net.AjaxRequest.onReadyState = function() {
    var req = this.req;
    var ready = req.readyState;
    if (ready == 4) {// 请求完成
        if (req.status == 200) {// 请求成功
            this.onload.call(this);
        } else {
            this.onerror.call(this);
        }
    }
}
// 重构默认的错误处理函数
net.AjaxRequest.prototype.defaultError = function() {
    alert("错误数据

回调状态:" + this.req.readyState + "
状态: " + this.req.status);
}
AjaxRequest.js
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="conn" class="com.caiduping.core.ConnDB" scope="page"></jsp:useBean>
<ul>
<%
//获取公告信息
ResultSet rs=conn.executeQuery("SELECT title FROM tb_bbsInfo ORDER BY id DESC");    
if(rs.next()){
    do{
        out.print("<li>"+rs.getString(1)+"</li>");
    }while(rs.next());
}else{
    out.print("<li>暂无公告信息!</li>");
}
%>

</ul>
getInfo.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<script language="javascript" src="JS/AjaxRequest.js"></script>
<script language="javascript">
/******************错误处理的方法*******************************/
function onerror(){
    alert("您的操作有误!");
}
/******************实例化Ajax对象的方法*******************************/
function getInfo(){
    var loader=new net.AjaxRequest("getInfo.jsp?nocache="+new Date().getTime(),deal_getInfo,onerror,"GET");
}
/************************回调函数**************************************/
function deal_getInfo(){
    document.getElementById("showInfo").innerHTML=this.req.responseText;
}
window.onload=function(){
    getInfo();    //调用getInfo()方法获取公告信息
    window.setInterval("getInfo()", 1000);    //每隔1秒调用一次getInfo()方法
}
</script>

<title>实时显示公告信息</title>
</head>
<body>
<div style="border: 2px solid;height: 350px; 350px;padding: 5px;color:#FF0000">
    <marquee direction="up" scrollamount="3">
        <div id="showInfo"></div>
    </marquee>
</div>
</body>
</html>
index.jsp

run:

不努力,还要青春干什么?
原文地址:https://www.cnblogs.com/caidupingblogs/p/5358054.html