【问题标题】:JSP JDBC MySQL Show Tables then Put Them to Select and UnionJSP JDBC MySQL 显示表然后将它们选择并联合
【发布时间】:2018-09-22 05:06:36
【问题描述】:

我正在尝试显示表格,然后将结果放入 mysql select 语句中。

这里是jsp代码:

<%@ page language="java" import="java.sql.*,java.util.*,java.text.*"%>

    <%
            String DRIVER = "com.mysql.jdbc.Driver";

            Class.forName(DRIVER).newInstance();
            Connection con = null;
            ResultSet rst = null;
            Statement stmt = null;
            Connection con2 = null;
            ResultSet rst2 = null;
            Statement stmt2 = null;     
            try {
                String url = "jdbc:mysql://localhost:3306/mydb?user=root&password=test";
                con = DriverManager.getConnection(url);
                stmt = con.createStatement();
                rst = stmt.executeQuery("select table_name from information_schema.tables where table_schema='mydb' and table_name like '%_work' ");

                con2 = DriverManager.getConnection(url);
                stmt2 = con2.createStatement();
                String table_name="table_name";


                while (rst.next()) {
                    ArrayList<String> list= new ArrayList<String>();

                    list.add(rst.getString("table_name"));  


                    for (String item : list) {                      

                        rst2 = stmt2.executeQuery("select distinct thecolumn from  '"+ item +"'  ");



                    }             
        }
        rst.close();
        stmt.close();
        con.close();
        } 
        catch (SQLException se) {
            //System.out.println("SQL statement is not executed!");
            se.printStackTrace();
        }
        catch (Exception e) {
                System.out.println(e.getMessage());
        }

    %>

我的问题:
请帮助我更正我的代码,使 rst2 变为:

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 ... ...);

【问题讨论】:

    标签: java mysql jsp arraylist jdbc


    【解决方案1】:

    这是一个使用 jstl 的示例:

    mySQL.jsp

    <%@ page language="java" import="java.sql.*,java.util.*,java.text.*" isELIgnored="false" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <%
        String DRIVER = "com.mysql.jdbc.Driver";
    
        ArrayList<String> tables = new ArrayList<String>();
    
        Class.forName(DRIVER).newInstance();
        Connection con = null;
        ResultSet rst = null;
        Statement stmt = null;
        Connection con2 = null;
        ResultSet rst2 = null;
        Statement stmt2 = null;     
        try {
            String url = "jdbc:mysql://localhost:3306/mydb?user=root&password=root&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
            con = DriverManager.getConnection(url);
            stmt = con.createStatement();
            rst = stmt.executeQuery("select table_name from information_schema.tables where table_schema='mydb' and table_name like '%_work' ");
    
            con2 = DriverManager.getConnection(url);
            stmt2 = con2.createStatement();
            String table_name = "table_name";
    
            //Insert into tables...
            while (rst.next()) {
                tables.add(rst.getString("table_name"));    
            }
    
            boolean first = true;
            StringBuilder query2 = new StringBuilder();
    
            //...then iterate over
            for (String table : tables) {           
                if(first) {
                  query2.append(" select distinct id from ");
                  first = false;
                }else {
                  query2.append(" UNION ALL select distinct id from ");
                }
                query2.append('\'').append(table).append('\'');
            }
    
            //Put variable inside context to access it in jstl
            pageContext.setAttribute("query", query2.toString());
            pageContext.setAttribute("tables", tables);
    
            rst.close();
            stmt.close();
            con.close();
        } catch (SQLException se) {
            //System.out.println("SQL statement is not executed!");
            se.printStackTrace();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }
    %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <style>
                table, th, td {
                   border: 1px solid black;
                }
            </style>
        </head>
        <body>
            <div>
                <span>Query : ${query}</span>
            </div>
            <table>
                <tr>
                  <th>Table name</th>
                </tr>
                <c:forEach var="table" items="${tables}"> 
                  <tr>
                    <td>
                        <c:out value = "${table}"/>
                    </td>
                  </tr>
                </c:forEach>
            </table>
        </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 2010-11-16
      • 2011-06-06
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多