Import Java.io.ioexception;import Java.io.inputstream;import java.sql.connection;import Java.sql.databasemetadata;import Java.sql.resultset;import Java.sql.sqlexception;import java.util.ArrayList; Import Java.util.list;import Java.util.properties;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.loushang.persistent.jdbc.datasource.PropertyDataSourceFactoryImpl; Public classDatabaseutil {Private StaticLog logger = Logfactory.getlog (databaseutil.class); Public StaticFinal String Datasource_filename ="datasource.properties";//data source files that connect to the database Public StaticFinal String Datasource_url ="Datasource.url";//key of the URL in the data source file Public StaticFinal String Datasource_username ="Datasource.username";//the key of the user name in the data source file /** * Read profile information * @return Properties profile Information*/ Public StaticProperties getProperties () {//InputStream in = Classloader.getsystemresourceasstream (datasource_filename);InputStreaminch= Propertydatasourcefactoryimpl.class. getClassLoader (). getResourceAsStream (Datasource_filename); if(inch==NULL) { inch=Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream (Datasource_filename); if(inch==NULL) {Logger.warn ("Can not find the DataSource config file ' datasource.properties '."); }} Properties Properties=NewProperties (); Try{properties.load (inch); } Catch(IOException e) {logger.error ("Error occurred when loading datasource config file.", E); } returnproperties;} /** * Read config file get database name of connected database * @return String database name*/ Public Staticstring GetDatabaseName () {string DatabaseName=""; Properties P=getProperties (); String Database=P.getproperty (Datasource_url); intStartIndex = Database.lastindexof (":"); DatabaseName= Database.substring (startindex+1, Database.length ()); returnDatabaseName;} /** * Read the configuration file to get the user name of the connection database * @return String user name*/ Public Staticstring Getuserofdatabase () {string user=""; Properties P=getProperties (); User=P.getproperty (datasource_username); returnuser;} /** * Get all table names for the specified database and user * @param conn Connection database object * @param user user * @param database name * @return*/ Public StaticList Getalltablenames (Connection conn, string user, String database) {List Tablenames=NewArrayList (); if(Conn! =NULL) { Try{DatabaseMetaData Dbmd=Conn.getmetadata (); //List of table namesResultSet rest = dbmd.gettables (database,NULL,NULL,NewString[] {"TABLE" }); //OUTPUT table_name while(Rest.next ()) {String Tableschem= Rest.getstring ("Table_schem"); if(User.equalsignorecase (Tableschem)) {Tablenames.add (rest.getstring ("table_name")); } } } Catch(SQLException e) {e.printstacktrace (); } } returnTablenames;} Public Static voidmain (String [] args) {Connection conn=NULL; Try{ /*String url= "jdbc:oracle:thin:@10.*.*.*:1521:***"; Class.forName ("Oracle.jdbc.driver.OracleDriver"); conn = drivermanager.getconnection (URL, "*", "*");*/DataSource DataSource= DataSourceFactory.defaultFactory.getDataSource ("DataSource"); Conn=datasource.getconnection (); } Catch(SQLException E1) {e1.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } List Tables=getalltablenames (conn, getuserofdatabase (), GetDatabaseName ()); System. out. println (Tables.size ()); }}