Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.61 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.nio.charset.Charset;
  5. import java.io.BufferedInputStream;
  6. import java.io.DataInputStream;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11.  
  12. public class dumper {
  13.     public static void main(String[] argv) throws SQLException {
  14.         String[] fieldSubstrings = {
  15.             "ssn",
  16.             "birth",
  17.             "dob","passw","auth","login","username","tax","report"
  18.         };
  19.  
  20.         List < String > connectionStrings = new ArrayList < String > (),
  21.             driverClasses = new ArrayList < String > (),
  22.             logins = new ArrayList < String > (),
  23.             passwords = new ArrayList < String > (),
  24.             result = new ArrayList < String > ();
  25.         int connectionsCount = 0;
  26.  
  27.         try {
  28.             BufferedReader in = new BufferedReader(new FileReader("input.txt"));
  29.             String line;
  30.             int lineNumber = 1;
  31.  
  32.             while ((line = in .readLine()) != null) {
  33.                 // System.out.println(line);
  34.  
  35.                 String[] lineSplitted = line.split("~");
  36.  
  37.                 if (lineSplitted.length != 4) {
  38.                     System.out.println("Incorrect data on line " + lineNumber + ". Skipped.");
  39.                     lineNumber++;
  40.                     continue;
  41.                 }
  42.  
  43.                 driverClasses.add(lineSplitted[0]);
  44.                 connectionStrings.add(lineSplitted[1]);
  45.                 logins.add(lineSplitted[2]);
  46.                 passwords.add(lineSplitted[3]);
  47.  
  48.                 connectionsCount++;
  49.                 lineNumber++;
  50.             }
  51.         } catch (Exception e) {
  52.             e.printStackTrace();
  53.             return;
  54.         }
  55.  
  56.         for (int i = 0; i < connectionsCount; i++) {
  57.             try {
  58.                 Class.forName(driverClasses.get(i));
  59.             } catch (ClassNotFoundException e) {
  60.                 e.printStackTrace();
  61.                 //return;
  62.             }
  63.  
  64.             // System.out.println(connectionStrings.get(i) + " " + logins.get(i) + " " + passwords.get(i));
  65.             Connection connection = null;
  66.             List < String > allColumns = new ArrayList < String > ();
  67.             Set currentTables = new HashSet();
  68.            
  69.             result = new ArrayList < String > ();
  70.  
  71.             try {
  72.                 connection = DriverManager.getConnection(connectionStrings.get(i), logins.get(i), passwords.get(i));
  73.             } catch (Exception e) {
  74.                 e.printStackTrace();
  75.                 continue;
  76.             }
  77.  
  78.             // Create directory if we're successfully connected.
  79.             String dirname = Integer.toString(i + 1);
  80.  
  81.             File dir = new File(dirname);
  82.             if (!dir.exists()) {
  83.                 try {
  84.                     dir.mkdir();
  85.                 } catch (Exception e) {
  86.                     e.printStackTrace();
  87.                     continue;
  88.                 }
  89.             }
  90.  
  91.             dir = new File(dirname);
  92.             if (dir.exists()) {
  93.                 try {
  94.                     PrintWriter writerCI = new PrintWriter(dirname + "/conninfo.txt", "UTF-8");
  95.                     writerCI.println(connectionStrings.get(i));
  96.                     writerCI.println(logins.get(i));
  97.                     writerCI.println(passwords.get(i));
  98.                     writerCI.close();
  99.                 } catch (Exception e) {
  100.                     e.printStackTrace();
  101.                     continue;
  102.                 }
  103.             }
  104.  
  105.             try {
  106.                 if (connection != null) {
  107.                     Statement statementTables = connection.createStatement();
  108.                     String query = "select OWNER, TABLE_NAME from ALL_TABLES";
  109.  
  110.                     java.sql.ResultSet recordsTables = statementTables.executeQuery(query);
  111.                     PrintWriter writerT = new PrintWriter(dirname + "/ALL_TABLES.txt", "UTF-8");
  112.  
  113.                     while (recordsTables.next()) {
  114.                         writerT.println(recordsTables.getString("OWNER") + "\t" + recordsTables.getString("TABLE_NAME"));
  115.                     }
  116.                     writerT.close();
  117.                     statementTables.close();
  118.  
  119.  
  120.  
  121.  
  122.                     Statement statementColumns = connection.createStatement();
  123.                     query = "select OWNER, TABLE_NAME, COLUMN_NAME from ALL_TAB_COLUMNS";
  124.                     java.sql.ResultSet recordsColumn = statementColumns.executeQuery(query);
  125.  
  126.                     PrintWriter writerCI = new PrintWriter(dirname + "/ALL_TAB_COLUMNS.txt", "UTF-8");
  127.                     while (recordsColumn.next()) {
  128.                         String owner = recordsColumn.getString("OWNER"),
  129.                             tableName = recordsColumn.getString("TABLE_NAME"),
  130.                             columnName = recordsColumn.getString("COLUMN_NAME");
  131.                         allColumns.add(owner + "\t" + tableName + "\t" + columnName);
  132.                        
  133.                         writerCI.println(owner + "\t" + tableName + "\t" + columnName);
  134.  
  135.  
  136.  
  137.                         // columnName.toLowerCase().contains(str2.toLowerCase())
  138.  
  139.                         boolean fieldFound = false;
  140.                         for (int j = 0; j < fieldSubstrings.length; j++) {
  141.                             if (columnName.toLowerCase().contains(fieldSubstrings[j].toLowerCase())) {
  142.                                 fieldFound = true;
  143.                             }
  144.                         }
  145.                        
  146.                         if (fieldFound && (!currentTables.contains(owner + "\t" + tableName))
  147.                                 && (!tableName.equals("ALL_PROCEDURES"))
  148.                                 && (!owner.equals("SYS"))
  149.                                 && (!owner.equals("MDSYS"))
  150.                                 && (!owner.startsWith("APEX_"))
  151.                                 && (!owner.contains("$"))) {
  152.                             currentTables.add(owner + "\t" + tableName);
  153.                             int count = 0;
  154.                             java.sql.ResultSet recordsCount;
  155.                             Statement statementCount;
  156.  
  157.                             try {
  158.                                 statementCount = connection.createStatement();
  159.  
  160.                                 query = "select COUNT(*) c from " + owner + "." + tableName;
  161.  
  162.                                 recordsCount = statementCount.executeQuery(query);
  163.                             } catch (Exception e) {
  164.                                 e.printStackTrace();
  165.                                 continue;
  166.                             }
  167.  
  168.                             try {
  169.                                 while (recordsCount.next()) {
  170.                                     count = Integer.parseInt(recordsCount.getString("c"));
  171.                                 }
  172.                             } catch (Exception e) {
  173.                                 e.printStackTrace();
  174.                             }
  175.  
  176.                             try {
  177.                                 statementCount.close();
  178.                             } catch (Exception e) {
  179.                                 e.printStackTrace();
  180.                             }
  181.  
  182.                             if (count > 0) {
  183.                                 result.add(Integer.toString(i + 1) + "\t" + owner + "\t" + tableName + "\t" + columnName + "\t" + Integer.toString(count));
  184.                             }
  185.                         }
  186.                     }
  187.                     writerCI.close();
  188.                     statementColumns.close();
  189.  
  190.                     PrintWriter writerAC = new PrintWriter(dirname + "/allcolumns.txt", "UTF-8");
  191.  
  192.                     for (Iterator < String > it = allColumns.iterator(); it.hasNext();) {
  193.                         String column = it.next();
  194.                         writerAC.println(column);
  195.                     }
  196.  
  197.                     writerAC.close();
  198.                 } else {
  199.                     System.out.println("Failed to make connection on " + connectionStrings.get(i));
  200.                 }
  201.             } catch (Exception e) {
  202.                 e.printStackTrace();
  203.                 continue;
  204.             }
  205.            
  206.             FileWriter fw = null;
  207.             BufferedWriter bw = null;
  208.             PrintWriter out = null;
  209.             try {
  210.                 fw = new FileWriter("total.txt", true);
  211.                 bw = new BufferedWriter(fw);
  212.                 out = new PrintWriter(bw);
  213.                
  214.                 for (Iterator < String > it = result.iterator(); it.hasNext();) {
  215.                     String row = it.next();
  216.                     out.println(row);
  217.                 }
  218.                
  219.                 out.close();
  220.             } catch (Exception e) {
  221.                 e.printStackTrace();
  222.                 continue;
  223.             }
  224.             finally {
  225.                 try {
  226.                     if (out != null) {
  227.                         out.close();
  228.                     }
  229.                 } catch (Exception e) {
  230.                     e.printStackTrace();
  231.                 }
  232.                 try {
  233.                     if (bw != null) {
  234.                         bw.close();
  235.                     }
  236.                 } catch (Exception e) {
  237.                     e.printStackTrace();
  238.                 }
  239.                 try {
  240.                     if (fw != null) {
  241.                         fw.close();
  242.                     }
  243.                 } catch (Exception e) {
  244.                     e.printStackTrace();
  245.                 }
  246.             }
  247.         }
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement