|
import java.io.File; |
|
import java.io.FileInputStream; |
|
import java.io.FileNotFoundException; |
|
import java.io.IOException; |
|
import java.sql.Connection; |
|
import java.sql.DriverManager; |
|
import java.sql.SQLException; |
|
import java.util.Properties; |
|
import java.sql.ResultSet; |
|
import java.sql.PreparedStatement; |
|
|
|
class TableCount |
|
{ |
|
public static void main(String[] args) |
|
{ |
|
TableCount t = new TableCount(); |
|
t.run(args); |
|
} |
|
|
|
public void run(String[] args) |
|
{ |
|
try |
|
{ |
|
Connection conn = getConnection(args[0], args[1], args[2]); |
|
getTableCount(conn, args[3]); |
|
conn.close(); |
|
} |
|
catch (Exception e) |
|
{ |
|
e.printStackTrace(); |
|
} |
|
} |
|
|
|
public static Connection getConnection(Properties p) throws Exception |
|
{ |
|
Connection conn = null; |
|
String username = p.getProperty("USERNAME"); |
|
String password = p.getProperty("PASSWORD"); |
|
String url = p.getProperty("URL"); |
|
String driver = p.getProperty("DRIVER"); |
|
|
|
System.out.println(username); |
|
System.out.println(url); |
|
|
|
Class.forName(driver); |
|
conn = DriverManager.getConnection(url, username, password); |
|
conn.setAutoCommit(false); |
|
|
|
return conn; |
|
} |
|
|
|
public static Connection getConnection(String file, String username, String password) throws Exception |
|
{ |
|
Properties props = loadProperties(file); |
|
props.setProperty("USERNAME", username); |
|
props.setProperty("PASSWORD", password); |
|
|
|
return getConnection(props); |
|
} |
|
|
|
private static Properties loadProperties(String file) throws FileNotFoundException, IOException |
|
{ |
|
Properties props = new Properties(); |
|
props.load(new FileInputStream(new File(file))); |
|
return props; |
|
} |
|
|
|
public void getTableCount(Connection conn, String owner) throws SQLException |
|
{ |
|
|
|
String sql = null; |
|
ResultSet rs = null; |
|
PreparedStatement stmt = null; |
|
|
|
sql = "SELECT count(*) c from all_tables where owner = ? "; |
|
|
|
try |
|
{ |
|
stmt = conn.prepareStatement(sql); |
|
stmt.setString(1, owner); |
|
rs = stmt.executeQuery(); |
|
while (rs.next()) |
|
{ |
|
long count = rs.getLong("c"); |
|
System.out.println("~~"); |
|
System.out.println(count); |
|
} |
|
} |
|
|
|
catch(SQLException exception) |
|
{ |
|
|
|
exception.printStackTrace(); |
|
} |
|
|
|
finally |
|
{ |
|
|
|
} |
|
} |
|
} |