No need to submit and modify Database Data and display the AJAX self-learning exercises

Source: Internet
Author: User

The request. jsp page contains rocarsId and ccrn text.
It corresponds to the msg_id and ccrn fields of the rocars table in the database. Now we need to modify the value of the ccrn on the interface. ajax is submitted to the response. jsp page, and the corresponding ccrn is updated by calling the RocarsEntiy. updateCcrn method.
Code:
Request. jsp Copy codeThe Code is as follows: <% @ 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. jspCopy codeThe Code is as follows: <% @ 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. javaCopy codeThe Code is as follows: 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.exe cuteQuery (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.exe cuteQuery (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.exe cuteUpdate (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
You can directly use JDBC or call the weblogic data source JNDI.Copy codeThe Code is as follows: 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;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.