Guest User

Untitled

a guest
Apr 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. <%@ page language="java" import="java.sql.*,java.util.*,java.text.*"%>
  2.  
  3. <%
  4. String DRIVER = "com.mysql.jdbc.Driver";
  5.  
  6. Class.forName(DRIVER).newInstance();
  7. Connection con = null;
  8. ResultSet rst = null;
  9. Statement stmt = null;
  10. Connection con2 = null;
  11. ResultSet rst2 = null;
  12. Statement stmt2 = null;
  13. try {
  14. String url = "jdbc:mysql://localhost:3306/mydb?user=root&password=test";
  15. con = DriverManager.getConnection(url);
  16. stmt = con.createStatement();
  17. rst = stmt.executeQuery("select table_name from information_schema.tables where table_schema='mydb' and table_name like '%_work' ");
  18.  
  19. con2 = DriverManager.getConnection(url);
  20. stmt2 = con2.createStatement();
  21. String table_name="table_name";
  22.  
  23.  
  24. while (rst.next()) {
  25. ArrayList<String> list= new ArrayList<String>();
  26.  
  27. list.add(rst.getString("table_name"));
  28.  
  29.  
  30. for (String item : list) {
  31.  
  32. rst2 = stmt2.executeQuery("select distinct thecolumn from '"+ item +"' ");
  33.  
  34.  
  35.  
  36. }
  37. }
  38. rst.close();
  39. stmt.close();
  40. con.close();
  41. }
  42. catch (SQLException se) {
  43. //System.out.println("SQL statement is not executed!");
  44. se.printStackTrace();
  45. }
  46. catch (Exception e) {
  47. System.out.println(e.getMessage());
  48. }
  49.  
  50. %>
  51.  
  52. rst2 = stmt2.executeQuery("select distinct thecolumn from table1_work UNION ALL select distinct thecolumn from table2_work UNION ALL select distinct thecolumn from table3_work" UNION ALL select distinct thecolumn from tablen_work UNION ALL etc ... ...);
  53.  
  54. <%@ page language="java" import="java.sql.*,java.util.*,java.text.*" isELIgnored="false" %>
  55. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  56.  
  57. <%
  58. String DRIVER = "com.mysql.jdbc.Driver";
  59.  
  60. ArrayList<String> tables = new ArrayList<String>();
  61.  
  62. Class.forName(DRIVER).newInstance();
  63. Connection con = null;
  64. ResultSet rst = null;
  65. Statement stmt = null;
  66. Connection con2 = null;
  67. ResultSet rst2 = null;
  68. Statement stmt2 = null;
  69. try {
  70. String url = "jdbc:mysql://localhost:3306/mydb?user=root&password=root&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  71. con = DriverManager.getConnection(url);
  72. stmt = con.createStatement();
  73. rst = stmt.executeQuery("select table_name from information_schema.tables where table_schema='mydb' and table_name like '%_work' ");
  74.  
  75. con2 = DriverManager.getConnection(url);
  76. stmt2 = con2.createStatement();
  77. String table_name = "table_name";
  78.  
  79. //Insert into tables...
  80. while (rst.next()) {
  81. tables.add(rst.getString("table_name"));
  82. }
  83.  
  84. //...then iterate over
  85. for (String item : tables) {
  86. rst2 = stmt2.executeQuery("select distinct id from " + item + " ");
  87. }
  88.  
  89. //Put variable inside context to access it in jstl
  90. pageContext.setAttribute("tables", tables);
  91.  
  92. rst.close();
  93. stmt.close();
  94. con.close();
  95. } catch (SQLException se) {
  96. //System.out.println("SQL statement is not executed!");
  97. se.printStackTrace();
  98. }
  99. catch (Exception e) {
  100. System.out.println(e.getMessage());
  101. }
  102. %>
  103.  
  104. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  105. <html>
  106. <head>
  107. <style>
  108. table, th, td {
  109. border: 1px solid black;
  110. }
  111. </style>
  112. </head>
  113. <body>
  114. <table>
  115. <tr>
  116. <th>Table name</th>
  117. </tr>
  118. <c:forEach var="table" items="${tables}">
  119. <tr>
  120. <td>
  121. <c:out value = "${table}"/>
  122. </td>
  123. </tr>
  124. </c:forEach>
  125. </table>
  126. </body>
  127. </html>
Add Comment
Please, Sign In to add comment