One of Mysql monitoring methods

The business system will eventually be fed back to the DB. By observing the changes in the DB, you can know the processing logic and processing method of the business system. The following code is used to create triggers for all MYSQL tables, combined with JSP to observe changes in DB. Attached is the renderings.
quote
The following SQL script does two things, generates a table that stores changes to records, and generates a stored procedure for triggering SQL

drop TABLE if EXISTS zz_modify;
CREATE TABLE `zz_modify` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` text,
  `createtime` datetime DEFAULT NULL,
  `tablename` varchar(4000) DEFAULT NULL,
  `oprtype` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- Stored procedure used to generate SQL for delete triggers.
drop PROCEDURE if EXISTS p_droptrgs;
CREATE PROCEDURE `p_droptrgs`()
BEGIN
-- define variables
declare tab_name varchar(400);
declare tmp_tabschema varchar(400) DEFAULT 'rmp_2';
declare v_sql_all text DEFAULT '';
declare cur_tab_done int DEFAULT 0;

-- define a traversal cursor for all tables
DECLARE cur_tab cursor for select table_name from information_schema.`TABLES` where TABLE_SCHEMA = tmp_tabschema and TABLE_NAME != 'zz_modify';

-- bind the end marker to the cursor
declare continue handler FOR SQLSTATE '02000' SET cur_tab_done = 1;

-- open cursor
OPEN cur_tab;
REPEAT
    FETCH cur_tab into tab_name;
    -- call p_createtrgsbytable(tmp_tabName, tmp_tabschema);
    set v_sql_all = CONCAT(v_sql_all,'drop trigger if EXISTS  trg_insert_', tab_name, '; \n');
    set v_sql_all = CONCAT(v_sql_all,'drop trigger if EXISTS  trg_update_', tab_name, '; \n');
    set v_sql_all = CONCAT(v_sql_all,'drop trigger if EXISTS  trg_delete_', tab_name, '; \n');
    UNTIL cur_tab_done
end REPEAT;
CLOSE cur_tab;

select v_sql_all;

END;

drop FUNCTION if EXISTS f_createtrgsbytable;
CREATE  FUNCTION `f_createtrgsbytable`(tab_name varchar(400), tab_schema varchar(400)) RETURNS text CHARSET utf8
    DETERMINISTIC
BEGIN
-- define variables
declare tmp_col_name varchar(4000);
DECLARE v_sql_all longtext DEFAULT '';
declare v_sql_insert longtext DEFAULT '';
declare v_sql_update longtext DEFAULT '';
declare v_sql_delete longtext DEFAULT '';


declare cur_tab_done int DEFAULT 0;

-- define a traversal cursor for all tables
DECLARE cur_tab cursor for select COLUMN_NAME from information_schema.`COLUMNS` where table_name = tab_name and TABLE_SCHEMA = tab_schema;

-- bind the end marker to the cursor
declare continue handler FOR SQLSTATE '02000' SET cur_tab_done = 1;

set v_sql_insert = CONCAT(v_sql_insert,'drop trigger if EXISTS  trg_insert_', tab_name, ';');
set v_sql_update = CONCAT(v_sql_update,'drop trigger if EXISTS  trg_update_', tab_name, ';');
set v_sql_delete = CONCAT(v_sql_delete,'drop trigger if EXISTS  trg_delete_', tab_name, ';');

set v_sql_insert = concat(v_sql_insert, 'create trigger trg_insert_', tab_name, ' after insert on ', tab_name, ' for each row begin insert into zz_modify(content, createtime, tablename, oprtype) value(concat(');
set v_sql_update = concat(v_sql_update, 'create trigger trg_update_', tab_name, ' after update on ', tab_name, ' for each row begin insert into zz_modify(content, createtime, tablename, oprtype) value(concat(');
set v_sql_delete = concat(v_sql_delete, 'create trigger trg_delete_', tab_name, ' after delete on ', tab_name, ' for each row begin insert into zz_modify(content, createtime, tablename, oprtype) value(concat(');

-- open cursor
OPEN cur_tab;
REPEAT
    FETCH cur_tab into tmp_col_name;
    set v_sql_insert = CONCAT(v_sql_insert, '\'#&old.', tmp_col_name, '=\',',  '\'-\'', ',\'~new.', tmp_col_name, '=\',', 'IFNULL(new.', tmp_col_name, ', \'nu-ll\'),');
    set v_sql_update = CONCAT(v_sql_update, '\'#&old.', tmp_col_name, '=\',',  'IFNULL(old.', tmp_col_name, ', \'nu-ll\') ,\'~new.', tmp_col_name, '=\',', 'IFNULL(new.', tmp_col_name, ', \'nu-ll\'),');
    set v_sql_delete = CONCAT(v_sql_delete, '\'#&old.', tmp_col_name, '=\',',  'IFNULL(old.', tmp_col_name, ', \'nu-ll\') ,\'~new.', tmp_col_name, '=\',', '\'-\'', ',');
    UNTIL cur_tab_done
end REPEAT;
CLOSE cur_tab;

-- Intercept processing
set v_sql_insert = CONCAT(SUBSTRING(v_sql_insert,1,LENGTH(v_sql_insert) - 1), ') ,now(), \'', tab_name, '\', \'insert\' ); end;\n');
set v_sql_update = CONCAT(SUBSTRING(v_sql_update,1,LENGTH(v_sql_update) - 1), ') ,now(), \'', tab_name, '\', \'update\' ); end;\n');
set v_sql_delete = CONCAT(SUBSTRING(v_sql_delete,1,LENGTH(v_sql_delete) - 1), ') ,now(), \'', tab_name, '\', \'delete\' ); end;\n');

return CONCAT(v_sql_insert,v_sql_update,v_sql_delete);

END;

-- Execute the stored procedure to generate the SQL that generates the trigger.
drop PROCEDURE if EXISTS p_createtrgs;
CREATE PROCEDURE `p_createtrgs`()
BEGIN
-- define variables
declare tmp_tabName varchar(400);
declare tmp_tabschema varchar(400) DEFAULT 'rmp_2';
declare v_sql_all longtext DEFAULT '';
declare cur_tab_done int DEFAULT 0;

-- define a traversal cursor for all tables
DECLARE cur_tab cursor for select table_name from information_schema.`TABLES` where TABLE_SCHEMA = tmp_tabschema and TABLE_NAME != 'zz_modify';

-- bind the end marker to the cursor
declare continue handler FOR SQLSTATE '02000' SET cur_tab_done = 1;

-- open cursor
OPEN cur_tab;
REPEAT
    FETCH cur_tab into tmp_tabName;
    -- call p_createtrgsbytable(tmp_tabName, tmp_tabschema);
    set v_sql_all = CONCAT(v_sql_all, f_createtrgsbytable(tmp_tabName, tmp_tabschema), '\n');
    UNTIL cur_tab_done
end REPEAT;
CLOSE cur_tab;


select v_sql_all;

END;


quote
JSP script to observe data changes

<!--First import some necessary packages-->
<%@page import="java.text.MessageFormat"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<!--Tell the compiler to use the SQL package-->
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.*" %>
<!--Set Chinese output-->
<%@ page contentType="text/html; charset=UTF-8"%>

<html>
<head>
<title>Data change observation</title>
<link rel="shortcut icon" href="http://a.fsdn.com/con/img/sftheme/favicon.ico">
</head>
<body>
 <div style="margin:10px;width:100%;heigth:50px;">
    <a href="del_modify.jsp" style="margin:0 5 0 5;">Clean up all data</a>
    <a href="comp.jsp" style="margin:0 5 0 5;">Query data changes</a>
</div>

	<%
		Connection con;
		Statement stmt;
		ResultSet rs;

		//Load the driver, the following code is to load the MySQL driver
		Class.forName("com.mysql.jdbc.Driver");

		//Register MySQL driver
		DriverManager.registerDriver(new com.mysql.jdbc.Driver());

		// connect to the database with the appropriate driver
		//String dbUrl = "jdbc:mysql://172.16.34.12:3306/cec?user=root&password=abcd1001&useUnicode=true&characterEncoding=UTF-8";
		//String dbUser = "cec"; //Username
		//String dbPwd = "cec"; //Password
		
		String dbUrl = "jdbc:mysql://192.168.9.139:3306/cec_yxt?user=root&password=abcd1001&useUnicode=true&characterEncoding=UTF-8";
		String dbUser = "root"; //Username
		String dbPwd = "123456"; //password
		//establish database connection
		con = java.sql.DriverManager.getConnection(dbUrl, dbUser, dbPwd);

		//Create a JDBC statement
		stmt = con.createStatement();

		//Query the records
		rs = stmt.executeQuery("select id,content,createtime,tablename,oprtype from zz_modify");

		// output query result
		
		int idx = 0;
		while (rs.next())
		{
			String content = rs.getString("content");
			String createtime = rs.getString("createtime");
			String tablename = rs.getString("tablename");
			String oprtype = rs.getString("oprtype");
			
			out.println("<table border=1  width='80%' style='margin-left:auto;margin-right:auto;margin-top:10px;margin-bottom:10px;'>");
			out.println(MessageFormat.format("<caption style='margin-top:10px;margin-bottom:10px;'>第【{0}】次表【{1}】在【{2}】被修改【{3}】。</caption>", ""+(++idx), tablename, createtime, oprtype));
			
			if (content == null || "".equals(content))
			{
				// print the displayed data
				out.println("<tr width='100%'>"
						+ "<td width='100%'>No modifications were obtained.</td>"
						+ "</tr>");
			}
			else
			{
				// print header
				out.println("<tr width='100%' style='word-break: break-all;'>"
				        + "<th width='5%'> 表字段 </td>"
						+ "<th width='20%'> old value</td>"
						+ "<th width='10%'> new value</td>"
						+ "</tr>");
				
				StringTokenizer st = new StringTokenizer(content, "#&");
				while (st.hasMoreTokens())
				{
					String row = st.nextToken();
					String[] rowarr = row.split("~");
					String oldContent = rowarr[0];
					String newContent = rowarr [1];
					String fieldName = oldContent.substring(oldContent.indexOf('.') + 1, oldContent.indexOf('='));
					String oldValue = oldContent.substring(oldContent.indexOf('=') + 1);
					String newValue = newContent.substring(newContent.indexOf('=') + 1);
				    
					if (newValue.equals(oldValue))
					{
						// print the displayed data
						out.println("<tr width='100%' style='word-break: break-all;'>"
						        + "<td width='5%'>" + fieldName + "</td>"
								+ "<td width='20%'>" + oldValue + "</td>"
								+ "<td width='10%'>" + newValue + "</td>"
								+ "</tr>");
					}
					else
					{
						// print the displayed data
						out.println("<tr width='100%' style='word-break: break-all;color:red;'>"
						        + "<td width='5%'>" + fieldName + "</td>"
								+ "<td width='20%'>" + oldValue + "</td>"
								+ "<td width='10%'>" + newValue + "</td>"
								+ "</tr>");
					}
				}
				
			}
			
			out.println("</table>");
		}
		

		//Close the database connection
		rs.close();
		stmt.close();
		con.close();
	%>
</body>
</html>

quote
The JSP script is used to delete all the data in the record table zz_modify.

<!--First import some necessary packages-->
<%@page import="java.text.MessageFormat"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<!--Tell the compiler to use the SQL package-->
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.*" %>
<!--Set Chinese output-->
<%@ page contentType="text/html; charset=UTF-8"%>

 <div style="margin:10px;width:100%;heigth:50px;">
    <a href="del_modify.jsp" style="margin:0 5 0 5;">Clean up all data</a>
    <a href="comp.jsp" style="margin:0 5 0 5;">Query data changes</a>
</div>

	<%
		Connection con;
		Statement stmt;
		ResultSet rs;

		//Load the driver, the following code is to load the MySQL driver
		Class.forName("com.mysql.jdbc.Driver");

		//Register MySQL driver
		DriverManager.registerDriver(new com.mysql.jdbc.Driver());

		// connect to the database with the appropriate driver
		//String dbUrl = "jdbc:mysql://172.16.34.12:3306/cec?user=root&password=abcd1001&useUnicode=true&characterEncoding=UTF-8";
		//String dbUser = "cec"; //Username
		//String dbPwd = "cec"; //Password
		
		String dbUrl = "jdbc:mysql://192.168.9.12:3306/cec_for_yxt_test?user=root&password=abcd1001&useUnicode=true&characterEncoding=UTF-8";
		String dbUser = "cec"; //Username
		String dbPwd = "n2h@5B_AoP"; //password
		//establish database connection
		con = java.sql.DriverManager.getConnection(dbUrl, dbUser, dbPwd);

		//Create a JDBC statement
		stmt = con.createStatement();

		//Query the records
		stmt.execute("delete from zz_modify");
		stmt.close();
		con.close();
	%>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326916182&siteId=291194637