(go) Get all the table names for the specified database and user

Source: Internet
Author: User

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 ()); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.