Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.*;
- import java.util.*;
- import java.nio.charset.Charset;
- import java.io.BufferedInputStream;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- public class dumper {
- public static void main(String[] argv) throws SQLException {
- String[] fieldSubstrings = {
- "ssn",
- "birth",
- "dob","passw","auth","login","username","tax","report"
- };
- List < String > connectionStrings = new ArrayList < String > (),
- driverClasses = new ArrayList < String > (),
- logins = new ArrayList < String > (),
- passwords = new ArrayList < String > (),
- result = new ArrayList < String > ();
- int connectionsCount = 0;
- try {
- BufferedReader in = new BufferedReader(new FileReader("input.txt"));
- String line;
- int lineNumber = 1;
- while ((line = in .readLine()) != null) {
- // System.out.println(line);
- String[] lineSplitted = line.split("~");
- if (lineSplitted.length != 4) {
- System.out.println("Incorrect data on line " + lineNumber + ". Skipped.");
- lineNumber++;
- continue;
- }
- driverClasses.add(lineSplitted[0]);
- connectionStrings.add(lineSplitted[1]);
- logins.add(lineSplitted[2]);
- passwords.add(lineSplitted[3]);
- connectionsCount++;
- lineNumber++;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return;
- }
- for (int i = 0; i < connectionsCount; i++) {
- try {
- Class.forName(driverClasses.get(i));
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- //return;
- }
- // System.out.println(connectionStrings.get(i) + " " + logins.get(i) + " " + passwords.get(i));
- Connection connection = null;
- List < String > allColumns = new ArrayList < String > ();
- Set currentTables = new HashSet();
- result = new ArrayList < String > ();
- try {
- connection = DriverManager.getConnection(connectionStrings.get(i), logins.get(i), passwords.get(i));
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- // Create directory if we're successfully connected.
- String dirname = Integer.toString(i + 1);
- File dir = new File(dirname);
- if (!dir.exists()) {
- try {
- dir.mkdir();
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- }
- dir = new File(dirname);
- if (dir.exists()) {
- try {
- PrintWriter writerCI = new PrintWriter(dirname + "/conninfo.txt", "UTF-8");
- writerCI.println(connectionStrings.get(i));
- writerCI.println(logins.get(i));
- writerCI.println(passwords.get(i));
- writerCI.close();
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- }
- try {
- if (connection != null) {
- Statement statementTables = connection.createStatement();
- String query = "select OWNER, TABLE_NAME from ALL_TABLES";
- java.sql.ResultSet recordsTables = statementTables.executeQuery(query);
- PrintWriter writerT = new PrintWriter(dirname + "/ALL_TABLES.txt", "UTF-8");
- while (recordsTables.next()) {
- writerT.println(recordsTables.getString("OWNER") + "\t" + recordsTables.getString("TABLE_NAME"));
- }
- writerT.close();
- statementTables.close();
- Statement statementColumns = connection.createStatement();
- query = "select OWNER, TABLE_NAME, COLUMN_NAME from ALL_TAB_COLUMNS";
- java.sql.ResultSet recordsColumn = statementColumns.executeQuery(query);
- PrintWriter writerCI = new PrintWriter(dirname + "/ALL_TAB_COLUMNS.txt", "UTF-8");
- while (recordsColumn.next()) {
- String owner = recordsColumn.getString("OWNER"),
- tableName = recordsColumn.getString("TABLE_NAME"),
- columnName = recordsColumn.getString("COLUMN_NAME");
- allColumns.add(owner + "\t" + tableName + "\t" + columnName);
- writerCI.println(owner + "\t" + tableName + "\t" + columnName);
- // columnName.toLowerCase().contains(str2.toLowerCase())
- boolean fieldFound = false;
- for (int j = 0; j < fieldSubstrings.length; j++) {
- if (columnName.toLowerCase().contains(fieldSubstrings[j].toLowerCase())) {
- fieldFound = true;
- }
- }
- if (fieldFound && (!currentTables.contains(owner + "\t" + tableName))
- && (!tableName.equals("ALL_PROCEDURES"))
- && (!owner.equals("SYS"))
- && (!owner.equals("MDSYS"))
- && (!owner.startsWith("APEX_"))
- && (!owner.contains("$"))) {
- currentTables.add(owner + "\t" + tableName);
- int count = 0;
- java.sql.ResultSet recordsCount;
- Statement statementCount;
- try {
- statementCount = connection.createStatement();
- query = "select COUNT(*) c from " + owner + "." + tableName;
- recordsCount = statementCount.executeQuery(query);
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- try {
- while (recordsCount.next()) {
- count = Integer.parseInt(recordsCount.getString("c"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- statementCount.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (count > 0) {
- result.add(Integer.toString(i + 1) + "\t" + owner + "\t" + tableName + "\t" + columnName + "\t" + Integer.toString(count));
- }
- }
- }
- writerCI.close();
- statementColumns.close();
- PrintWriter writerAC = new PrintWriter(dirname + "/allcolumns.txt", "UTF-8");
- for (Iterator < String > it = allColumns.iterator(); it.hasNext();) {
- String column = it.next();
- writerAC.println(column);
- }
- writerAC.close();
- } else {
- System.out.println("Failed to make connection on " + connectionStrings.get(i));
- }
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- FileWriter fw = null;
- BufferedWriter bw = null;
- PrintWriter out = null;
- try {
- fw = new FileWriter("total.txt", true);
- bw = new BufferedWriter(fw);
- out = new PrintWriter(bw);
- for (Iterator < String > it = result.iterator(); it.hasNext();) {
- String row = it.next();
- out.println(row);
- }
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
- finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- if (bw != null) {
- bw.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- if (fw != null) {
- fw.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement