Java reads data source information, and java reads related information.

Source: Internet
Author: User

Java reads data source information, and java reads related information.
I. package com. ofsp. utils; import java. io. IOException; import java. io. inputStream; import java. SQL. connection; import java. SQL. databaseMetaData; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. util. arrayList; import java. util. list; import java. util. properties; public class performanceutil {public static final String performance_filename = "jdbc. properties "; // public static final String performance_url =" jdbc. url "; // key public static final String performance_username =" jdbc. username "; // key public static final String performance_password =" jdbc. password "; // The key/*** of the password in the data source file reads the configuration file information * @ return Properties configuration file information * @ throws Exception */public static Properties getProperties () throws Exception {InputStream in = null; if (in = null ){//Class. getResourceAsStream () specifies the path of the resource to be loaded to be consistent with the path of the package of the current Class.For example, if you write a MyTest class under the com. test. mycode package, then MyTest. class.getResourceAsStream("name")Find the corresponding resource under the com. test. mycode package. If the name starts with '/', it will start from the root path of classpath. ClassLoader. getResourceAsStream () is located in the root path of classpath no matter whether '/' is included in the resource to be searched. In = Thread. currentThread (). getContextClassLoader (). getResourceAsStream (performance_filename); // The path is the relative path of classpath,ClasspathYesWEB-INFFolderClassesDirectoryIf (in = null) {throw new Exception ("the data source configuration file with the path" + performance_filename + "cannot be found. ") ;}} Properties properties = new Properties (); try {properties. load (in);} catch (IOException e) {e. printStackTrace ();} return properties;}/*** read the configuration file to obtain the database url to connect to the database * @ return String database name * @ throws Exception */public static String getDatabaseURL () throws Exception {Properties p = getProperties (); return p. getProperty (performance_url);}/*** read the configuration file and obtain the username * @ return String username * @ throw S Exception */public static String getUserOfDatabase () throws Exception {Properties p = getProperties (); return p. getProperty (performance_username);}/*** read the configuration file to obtain the password for connecting to the database * @ return String password * @ throws Exception */public static String getPasswordOfDatabase () throws Exception {Properties p = getProperties (); return p. getProperty (performance_password);}/*** get database Connection * @ return * @ throws Exception */public static Connection getConnection () throws Exception {Properties p = getProperties (); String url = p. getProperty (performance_url); String username = p. getProperty (performance_username); String password = p. getProperty (performance_password); return DriverManager. getConnection (url, username, password);}/*** get all table names of the specified database and user * @ param conn connect database object * @ param user * @ param database Database name * @ return */public static List <String> getAllTableNames (Connection conn, String database, String schem, String tableName) {List <String> tableNames = new ArrayList <String> (); if (conn! = Null) {try {DatabaseMetaData metadata = conn. getMetaData (); // Table Name List ResultSet rest = metadata. getTables (database, schem. toUpperCase (), tableName. toUpperCase (), new String [] {"TABLE"}); while (rest. next () {tableNames. add (rest. getString ("TABLE_NAME") ;}} catch (SQLException e) {e. printStackTrace () ;}} return tableNames ;}ii. If the Spring framework is used, you can also get the current data source from the Spring configuration file: the data source file is configured in the spring-context.xml Part: DruidDataSource = ApplicationContextHelper. getBean ("dataSource"); // obtain the data source Connection = dataSource. getConnection (); // obtain the connection String user = dataSource. getUsername (); // obtain the user name. Note: close the connection once it is used up. Otherwise, an overflow error occurs.

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.