Application and Reflection of data pools

Source: Internet
Author: User

 

Data Pool Application

Private Static datasource = NULL;

Static {// static code block Initialization

// The key-Value Pair read from the configuration file by the collection object

Properties prop = new properties ("dbcp-config.properties "));

Inputstream in = mydatasource. Class. getclassloader (). getresourceasstream ();

Prop. Load (in );

// Datasource: data source object for database operations

Datasource = basicdatasourcefactory. createdatasource (PROP );

}

// Obtain the connection

Public static connection getconnection () throws sqlexception {

Return datasource. getconnection (); // get the connection from the data source, that is, get one from the pool.

}

// Release resources

Public static void release (resultset RS, statement St, connection conn ){

If (RS! = NULL)

Try {

Rs. Close ();

} Catch (sqlexception e ){

E. printstacktrace ();

}

If (st! = NULL)

Try {

St. Close ();

} Catch (sqlexception e ){

E. printstacktrace ();

}

If (Conn! = NULL)

Try {

Conn. Close ();

} Catch (sqlexception e ){

E. printstacktrace ();

}

}

Reflection

Reflection mainly refers to the ability of programmers to access, detect and modify their own States or behaviors. In the computer field, reflection refers to a type of application, which can be self-described and sub-controlled. A mechanism is used to describe and monitor behaviors, and according to the status and results of behaviors, adjusts or modifies the status and related statements of the behavior described by the application.

Reflection allows dynamic discovery and binding of classes, methods, fields, and all other elements generated by languages. Reflection can be used to list classes, fields, and methods. Create instances, call methods, and access fields at runtime. Reflection is the key to Java being regarded as a dynamic (or standard dynamic) language.

Reflection mechanism features:

1. Determine the class to which any object belongs during running.

2. Construct the object of any class at runtime.

3. Determine the member variables and methods of any class at runtime.

4. Call the methods of any object at runtime. You can call the private method through reflection.

5. Generate a dynamic proxy.

Fields, constructor, method, and array classes in Java. Lang. Class and Java. Lang. Reflect packages:

1. The class is the origin of Java reflection. The class instance indicates the classes and interfaces of running Java applications.

2. The field class encapsulates the attributes of the reflection class.

3. the constructor class encapsulates the reflection method.

4. The method class encapsulates an instance of the reflection method.

5. The Aray class provides static methods for creating arrays and accessing arrays. All methods of this class are static.

JVM manages a unique Class Object for each type, which stores the type information of the corresponding class or interface. To obtain information about a class or interface, you must first obtain this class object.

Get Class Object

1. Call the getclass () method of the object class to obtain the Class Object. This is also the most common method for generating class objects. For example,
Myobject x = new myobject ();
Class C1 = x. getclass ();

2. Use the forname () Static Method of the class to obtain the class object corresponding to the string. Class C2 = Class. forname ("Java. Lang. String ");
Note: The parameter string must be a fully qualified name of a class or interface.

3. Use the type name. Class to obtain the class object corresponding to the type. For example, class cl1 = manager. Class;
Class Cl2 = int. Class; Class l3 = double []. Class;

To obtain information of a certain type at runtime, you mustFirst obtain the class object corresponding to this typeAnd then call the corresponding method provided by the class to obtain it.

1. Get the class object corresponding to the specified class
Class clazz = Class. forname ("Java. util. arraylist ");

2. Get the package name clazz. getpackage () of the specified class to get the information about the package where the object corresponding to this class object is located. It is an object of the Java. Lang. Package class.
String packagename = clazz. getpackage (). getname ();

3. Get the modifier of the specified class
The getmodifiers () method of the class object can be used to obtain the class modifier value of the object corresponding to this class object, represented by an integer.
Int mod = clazz. getmodifiers ();
String modifier = modifier. tostring (MOD );

4. Get the fully qualified name of the specified class
Classname = clazz. getname ();

5. Obtain the parent class of the specified class
Superclazz = clazz. getsuperclass ();

6. Obtain the interface implemented by the specified class
The getinterfaces () method of the class object can be used to obtain the Class Object array of all interfaces implemented by the object corresponding to this class object. Class [] interfaces = clazz. getinterfaces ();

7. Get the member variables of the specified class
You can use the getfields () method of the Class Object to obtain all the public fields (member variables) of the object corresponding to this class object ). To obtain all fields, use the getdeclaredfields () method.

Obtain the class Constructor

Constructor [] constrcutors = clazz. getdeclaredconstructors ();

Obtain the class member Method

Method [] Methods = clazz. getdeclaredmethods ();

Create object

Use the construction method without Parameters

List list = (list) C. newinstance ();

Use the construction method with Parameters

Step 2: Obtain the class object corresponding to the specified class.

Step 2: Use the class object to obtain the constructor object that meets the specified parameter type requirements.

Step 2 call the newinstance method of the specified constructor object, input the corresponding parameter value, and create the object.

 

JavaPropertiesA configuration file is used to express configuration information. The file type is *. properties, and the format isText FilesThe content of the file is in the format of "Key = value". That is to say, each row of the file first defines a key name, followed by a value after the equals sign. In the properties file, you can use "#" for comments,

 

The properties class in JDK exists in the Java. util package and inherits from hashtable. The main methods include:

1. getproperty (string key): Use the specified key to search for attributes in this attribute list. That is, the value corresponding to the key is obtained through the key parameter.

2. Load (inputstream instream): Read the attribute list (key and element pair) from the input stream ). Load the specified file to obtain all key-value pairs in the file for getproperty (string key) to search.

3. setproperty (string key, string value): Call the hashtable method put. To set the "key-value" pair.

4. Store (outputstream out, string comments): writes the attribute list (key and element pair) in the properties table to the output stream in a format suitable for loading to the properties table using the load method.

5. Clear (): Clear all mounted "key-value" pairs.

 

 

 

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.