一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

AJAX 自學(xué)練習(xí) 無(wú)刷新提交并修改數(shù)據(jù)庫(kù)數(shù)據(jù)并顯示

request.jsp頁(yè)面中有rocarsId,和ccrn兩個(gè)text。
對(duì)應(yīng)在數(shù)據(jù)庫(kù)中表格 rocars表的msg_id,ccrn兩個(gè)字段。現(xiàn)在要實(shí)現(xiàn)在界面上修改ccrn的值,ajax提交到response.jsp頁(yè)面,并調(diào)用RocarsEntiy.updateCcrn方法更新對(duì)應(yīng)的ccrn,最后無(wú)刷新顯示
代碼:
request.jsp
復(fù)制代碼 代碼如下:
<%@ page language="Java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script language="Javascript"><!--
function GetXmlHttpObject(){
    var xmlHttp = null;
    try{
        xmlHttp = new XMLHttpRequest();
    }catch(e){
        try{
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;

}
function updateCcrn(rocarsId,ccrn){
alert(rocarsId)
alert(ccrn)
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert ("you browser don't support the ajax");
         return;
    }
    var url = "./response.jsp";
    url = url + "?rocarsId="+ rocarsId;
    url = url + "&ccrn="+ ccrn;
    url = url + "&sid ="+ Math.random();
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}
function getCcrn(str){
    xmlHttp = GetXmlHttpObject();
    if(xmlHttp == null){
        alert ("you browser don't support the ajax");
         return;

    }
    var url = "./response.jsp";
    url = url + "?q="+ str;
    url = url + "&sid ="+ Math.random();
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}
function stateChanged()
{
    if(xmlHttp.readyState==4)
    {
        document.getElementById("rocarsccrn").value = xmlHttp.responseText;
    }
}
// --></script>
</head>
<body>
<form name="form1" action="" method="post">
    <label>rocarsId:</label><input type="text" name="rocarsId" value="140" />
    <label>ccrn:</label><input type="text" id="rocarsccrn" name="rocarsccrn" onchange="updateCcrn(document.form1.rocarsId.value,this.value)"/>
</form>
</body>
</html>

response.jsp
復(fù)制代碼 代碼如下:
<%@ page language="Java" contentType="text/plain; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.lwf.eus.util.*,Java.util.*,com.lwf.eus.entity.*,com.lwf.eus.bean.*" %>
<%
    String rocarsId = request.getParameter("rocarsId");
    String ccrn = request.getParameter("ccrn");
    System.out.println("rocarsId:" + rocarsId);
    System.out.println("ccrn:" + ccrn);
    RocarsEntity.updateCcrnById(rocarsId,ccrn);
    out.print(ccrn);
%>

RocarsEntity.Java
復(fù)制代碼 代碼如下:
package com.lwf.eus.entity;
import Java.sql.Connection;
import Java.sql.ResultSet;
import Java.sql.SQLException;
import Java.sql.Statement;
import Java.util.Vector;
import com.lwf.eus.bean.RocarsBean;
import com.lwf.eus.util.ConnectionManager;
public class RocarsEntity {
    public static Vector getRocarsList() {
        Vector vRocars = new Vector();
        //Connection conn = ConnectionManager.getConnection();
        Connection conn = ConnectionManager.getConnectionFromDS();
        Statement st = ConnectionManager.createStatement(conn);
        String sql = "select msg_id,ccrn from rocars";
        ResultSet rs = null;
        try {
            rs = st.executeQuery(sql);
            while (rs.next()) {
                RocarsBean rocars = new RocarsBean();
                rocars.setRocarsId(rs.getString(1));
                rocars.setCcrn(rs.getString(2));
                vRocars.addElement(rocars);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return vRocars;
        }

    }

    public static RocarsBean getRocarsListById(String rocarsId){
        Connection conn = ConnectionManager.getConnectionFromDS();
        Statement st = ConnectionManager.createStatement(conn);
        String sql = "select * from rocars where msg_id=" + rocarsId;
        ResultSet rs = null;
        RocarsBean rocars= null;
        try {
            rs = st.executeQuery(sql);
            rocars = new RocarsBean();
            while (rs.next()) {                
                rocars.setRocarsId(rs.getString("msg_id"));
                rocars.setCcrn(rs.getString("ccrn"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return rocars;
        }
    }
    public static void updateCcrnById(String rocarsId, String ccrn)
    {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = ConnectionManager.getConnectionFromDS();
            stmt = ConnectionManager.createStatement(conn);
            String sqlStr = "update rocars set ccrn= '"+ ccrn + "'" + "where msg_id=" + rocarsId ;
            stmt.executeUpdate(sqlStr);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{

            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

ConnectionManager.Java
這里可以直接使用JDBC也可以調(diào)用weblogic的數(shù)據(jù)源JNDI。
復(fù)制代碼 代碼如下:
package com.lwf.eus.util;
import Java.sql.Connection;
import Java.sql.DriverManager;
import Java.sql.SQLException;
import Java.sql.Statement;
import Javax.naming.Context;
import Javax.naming.InitialContext;
import Javax.naming.NamingException;
import Javax.sql.DataSource;
public class ConnectionManager {
    public static Connection getConnection(){
        Connection conn = null;
        String url = "jdbc:postgresql://192.168.0.180/getseus";
        String userName = "getsdbadmin";
        String pwd = "powerdb";
        try {
            Class.forName("org.postgresql.Driver").newInstance();
            conn = DriverManager.getConnection(url,userName,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }

        return conn;
    }

    public static Connection getConnectionFromDS(){
        Connection conn = null;
        Context ctx = null;
        try {
            ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("JNDIPG");
            conn = ds.getConnection();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return conn;

    }

    public static Statement createStatement(Connection conn){
        Statement st = null;
            try {
                st = conn.createStatement();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        return st;
    }
}

jsp技術(shù)AJAX 自學(xué)練習(xí) 無(wú)刷新提交并修改數(shù)據(jù)庫(kù)數(shù)據(jù)并顯示,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产成人综合日韩精品婷婷九月 | 久久精品免视着国产成人 | 5566成人免费视频观看 | 正在播放亚洲一区 | 狠狠色狠狠色综合网 | 伊人亚洲综合 | 精品四虎免费观看国产高清 | 中文字幕视频一区 | 91华人在线视频 | 中文字幕第二一区 | 久久久久国产免费 | 好吊操妞 | 亚洲一区二区高清 | 色播亚洲精品网站 亚洲第一 | 精品在线观看国产 | 国产成人禁片在线观看 | 色视频在线网站 | 国产精品国内免费一区二区三区 | 亚洲国产成人在线 | 绝对真实国产乱 | 91久久青草精品38国产 | 韩国精品一区视频在线播放 | 国产在线更新 | 国产产一区二区三区久久毛片国语 | 久久精品视香蕉蕉er大臿蕉 | 国产综合激情在线亚洲第一页 | 国产高清免费视频 | 色婷婷久久 | 亚洲图片另类图片 | 精品中文字幕在线 | 国产三级级在线观看大学生 | 国产成人在线观看网站 | www.草草草| 色婷婷在线播放 | 成人精品综合免费视频 | 欧美一级网址 | 视频二区中文字幕 | 男人日女人的免费视频 | 国产一级免费在线观看 | 成年美女黄网站色大片软件 | 精品一区二区三区五区六区七区 |