ASCII码 ASCII码

AJAX 自学练习 无刷新提交并修改数据库数据并显示

发布于:2022-04-02 10:37:13  栏目:技术文档
  1. AJAX 自学练习 无刷新提交并修改数据库数据并显示
  1. 对应在数据库中表格 rocars表的msg_id,ccrn两个字段。现在要实现在界面上修改ccrn的值,ajax提交到response.jsp页面,并调用RocarsEntiy.updateCcrn方法更新对应的ccrn,最后无刷新显示
  2. request.jsp页面中有rocarsId,和ccrn两个text。
  3. 对应在数据库中表格 rocars表的msg_id,ccrn两个字段。现在要实现在界面上修改ccrn的值,ajax提交到response.jsp页面,并调用RocarsEntiy.updateCcrn方法更新对应的ccrn,最后无刷新显示
  4. 代码:
  5. request.jsp
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. <script language="javascript"><!--
  9. function GetXmlHttpObject(){
  10. var xmlHttp = null;
  11. try{
  12. xmlHttp = new XMLHttpRequest();
  13. }catch(e){
  14. try{
  15. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  16. }catch(e){
  17. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  18. }
  19. }
  20. return xmlHttp;
  21. }
  22. function updateCcrn(rocarsId,ccrn){
  23. alert(rocarsId)
  24. alert(ccrn)
  25. xmlHttp = GetXmlHttpObject();
  26. if(xmlHttp == null){
  27. alert ("you browser don't support the ajax");
  28. return;
  29. }
  30. var url = "./response.jsp";
  31. url = url + "?rocarsId="+ rocarsId;
  32. url = url + "&ccrn="+ ccrn;
  33. url = url + "&sid ="+ Math.random();
  34. xmlHttp.onreadystatechange = stateChanged;
  35. xmlHttp.open("GET", url, true);
  36. xmlHttp.send(null);
  37. }
  38. function getCcrn(str){
  39. xmlHttp = GetXmlHttpObject();
  40. if(xmlHttp == null){
  41. alert ("you browser don't support the ajax");
  42. return;
  43. }
  44. var url = "./response.jsp";
  45. url = url + "?q="+ str;
  46. url = url + "&sid ="+ Math.random();
  47. xmlHttp.onreadystatechange = stateChanged;
  48. xmlHttp.open("GET", url, true);
  49. xmlHttp.send(null);
  50. }
  51. function stateChanged()
  52. {
  53. if(xmlHttp.readyState==4)
  54. {
  55. document.getElementById("rocarsccrn").value = xmlHttp.responseText;
  56. }
  57. }
  58. // --></script>
  59. </head>
  60. <body>
  61. <form name="form1" action="" method="post">
  62. <label>rocarsId:</label><input type="text" name="rocarsId" value="140" />
  63. <label>ccrn:</label><input type="text" id="rocarsccrn" name="rocarsccrn" onchange="updateCcrn(document.form1.rocarsId.value,this.value)"/>
  64. </form>
  65. </body>
  66. </html>
  1. response.jsp
  1. <%@ page language="java" contentType="text/plain; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ page import="com.lwf.eus.util.*,java.util.*,com.lwf.eus.entity.*,com.lwf.eus.bean.*" %>
  4. <%
  5. String rocarsId = request.getParameter("rocarsId");
  6. String ccrn = request.getParameter("ccrn");
  7. System.out.println("rocarsId:" + rocarsId);
  8. System.out.println("ccrn:" + ccrn);
  9. RocarsEntity.updateCcrnById(rocarsId,ccrn);
  10. out.print(ccrn);
  11. %>
  1. RocarsEntity.java
  1. package com.lwf.eus.entity;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Vector;
  7. import com.lwf.eus.bean.RocarsBean;
  8. import com.lwf.eus.util.ConnectionManager;
  9. public class RocarsEntity {
  10. public static Vector getRocarsList() {
  11. Vector vRocars = new Vector();
  12. //Connection conn = ConnectionManager.getConnection();
  13. Connection conn = ConnectionManager.getConnectionFromDS();
  14. Statement st = ConnectionManager.createStatement(conn);
  15. String sql = "select msg_id,ccrn from rocars";
  16. ResultSet rs = null;
  17. try {
  18. rs = st.executeQuery(sql);
  19. while (rs.next()) {
  20. RocarsBean rocars = new RocarsBean();
  21. rocars.setRocarsId(rs.getString(1));
  22. rocars.setCcrn(rs.getString(2));
  23. vRocars.addElement(rocars);
  24. }
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. } finally {
  28. try {
  29. if (rs != null) {
  30. rs.close();
  31. }
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. try {
  36. if (st != null) {
  37. st.close();
  38. }
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. }
  42. try {
  43. if (conn != null) {
  44. conn.close();
  45. }
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. return vRocars;
  50. }
  51. }
  52. public static RocarsBean getRocarsListById(String rocarsId){
  53. Connection conn = ConnectionManager.getConnectionFromDS();
  54. Statement st = ConnectionManager.createStatement(conn);
  55. String sql = "select * from rocars where msg_id=" + rocarsId;
  56. ResultSet rs = null;
  57. RocarsBean rocars= null;
  58. try {
  59. rs = st.executeQuery(sql);
  60. rocars = new RocarsBean();
  61. while (rs.next()) {
  62. rocars.setRocarsId(rs.getString("msg_id"));
  63. rocars.setCcrn(rs.getString("ccrn"));
  64. }
  65. } catch (SQLException e) {
  66. e.printStackTrace();
  67. } finally {
  68. try {
  69. if (rs != null) {
  70. rs.close();
  71. }
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74. }
  75. try {
  76. if (st != null) {
  77. st.close();
  78. }
  79. } catch (SQLException e) {
  80. e.printStackTrace();
  81. }
  82. try {
  83. if (conn != null) {
  84. conn.close();
  85. }
  86. } catch (SQLException e) {
  87. e.printStackTrace();
  88. }
  89. return rocars;
  90. }
  91. }
  92. public static void updateCcrnById(String rocarsId, String ccrn)
  93. {
  94. Connection conn = null;
  95. Statement stmt = null;
  96. try {
  97. conn = ConnectionManager.getConnectionFromDS();
  98. stmt = ConnectionManager.createStatement(conn);
  99. String sqlStr = "update rocars set ccrn= '"+ ccrn + "'" + "where msg_id=" + rocarsId ;
  100. stmt.executeUpdate(sqlStr);
  101. } catch (SQLException e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. } finally{
  105. try {
  106. if (stmt != null) {
  107. stmt.close();
  108. }
  109. } catch (SQLException e) {
  110. e.printStackTrace();
  111. }
  112. try {
  113. if (conn != null) {
  114. conn.close();
  115. }
  116. } catch (SQLException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120. }
  121. }
  1. ConnectionManager.java
  2. 这里可以直接使用JDBC也可以调用weblogic的数据源JNDI。
  1. package com.lwf.eus.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import javax.naming.Context;
  7. import javax.naming.InitialContext;
  8. import javax.naming.NamingException;
  9. import javax.sql.DataSource;
  10. public class ConnectionManager {
  11. public static Connection getConnection(){
  12. Connection conn = null;
  13. String url = "jdbc:postgresql://192.168.0.180/getseus";
  14. String userName = "getsdbadmin";
  15. String pwd = "powerdb";
  16. try {
  17. Class.forName("org.postgresql.Driver").newInstance();
  18. conn = DriverManager.getConnection(url,userName,pwd);
  19. } catch (SQLException e) {
  20. e.printStackTrace();
  21. } catch (ClassNotFoundException e) {
  22. e.printStackTrace();
  23. } catch (InstantiationException e) {
  24. e.printStackTrace();
  25. } catch (IllegalAccessException e) {
  26. e.printStackTrace();
  27. } catch (Exception e){
  28. e.printStackTrace();
  29. }
  30. return conn;
  31. }
  32. public static Connection getConnectionFromDS(){
  33. Connection conn = null;
  34. Context ctx = null;
  35. try {
  36. ctx = new InitialContext();
  37. DataSource ds = (DataSource)ctx.lookup("JNDIPG");
  38. conn = ds.getConnection();
  39. } catch (NamingException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. } catch (SQLException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. }
  46. return conn;
  47. }
  48. public static Statement createStatement(Connection conn){
  49. Statement st = null;
  50. try {
  51. st = conn.createStatement();
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. return st;
  56. }
  57. }
相关推荐
阅读 +