AJAX 自学练习 无刷新提交并修改数据库数据并显示
程序员文章站
2024-02-15 21:51:47
request.jsp页面中有rocarsid,和ccrn两个text。 对应在数据库中表格 rocars表的msg_id,ccrn两个字段。现在要实现在界面上修改ccrn...
request.jsp页面中有rocarsid,和ccrn两个text。
对应在数据库中表格 rocars表的msg_id,ccrn两个字段。现在要实现在界面上修改ccrn的值,ajax提交到response.jsp页面,并调用rocarsentiy.updateccrn方法更新对应的ccrn,最后无刷新显示
代码:
request.jsp
<%@ 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
<%@ 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
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也可以调用weblogic的数据源jndi。
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;
}
}
对应在数据库中表格 rocars表的msg_id,ccrn两个字段。现在要实现在界面上修改ccrn的值,ajax提交到response.jsp页面,并调用rocarsentiy.updateccrn方法更新对应的ccrn,最后无刷新显示
代码:
request.jsp
复制代码 代码如下:
<%@ 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
复制代码 代码如下:
<%@ 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
复制代码 代码如下:
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也可以调用weblogic的数据源jndi。
复制代码 代码如下:
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;
}
}
上一篇: 2020-09-07 作业