Today, test how Java DB is connected.
There are two types of knowledge that I know at the moment:
-
JDBC Code Links
DataSource Links
Let's look at the two ways to connect in more detail below
The first JDBC connection, which we need to know about Java's related class Javax.sql class
Package Javax.sql
Provides APIs for server-side data source access and processing through the Java programming language
Interface Summary
Commondatasource This interface defines a common method between DataSource, Xadatasource, and Connectionpolldatasource
1. All sub-interfaces ConnectionPoolDataSource DataSource Xadatasource
2. Summary of methods
A. int getlogintimeout () Gets the maximum amount of time, in seconds, that this data source can wait to connect to a database
B.printwriter Getlogwriter () gets this DataSource object log write.
C.void setlogintimeout (int seconds) sets the maximum time, in seconds, that this data source will wait when attempting to connect to a database
D. Setlogwriter (PrintWriter out) sets the log writer for this DataSource object to the given Java.io.PrintWriter
The above is the method of the Super interface Commondatasource, and the known sub-interface
Here's a three sub-interface.
ConnectionPoolDataSource
Pooledconnection Object Factory
Method is inherited by the parent class Commondatasource method Getlogintimeout setlogintimeout Setlogwriter Getlogwriter, its own methods are:
Pooledconnection getpooledconnection () attempts to establish a physical database connection that can be used as pooled connection
Pooledconnection getpooledconnection (String usr,string Password) attempts to establish a physical database connection that can be used as a pooled connection
Interface DataSource
DataSource This interface not only inherits the Commondatasource but also inherits the wrapper
The factory is used to provide a connection to the physical data source represented by this DataSource object. As an alternative to the DriverManager tool, the DataSource object is the preferred method for getting a connection. Objects that implement the DataSource interface are typically registered in a naming service that is based on the Java naming and Directory Interface (JNDI) API.
- The
-
DataSource interface is implemented by the driver vendor. A total of three types are implemented:
-
Basic implementation-Generate standard connection objects
-
Connection pool Implementation-Generate auto-ginseng Jade The connection object for the connection pool, which is implemented with the middle-tier connection pool manager using the
-
Distributed transaction Implementation-generates an connection object that can be used for distributed transactions, and in most cases is always involved in connection pooling. This implementation is used with the middle-tier transaction manager and is always used with the connection pool manager in most cases. The properties of the
-
DataSource object can be modified when necessary. GCA, if you move the data source to another server, you can change the properties that are related to the server. The advantage is that because you can change the properties of the data source, any code that accesses the data source does not have to change the driver that
-
Accesses through the DataSource object itself and does not register with DriverManager The DataSource object is obtained by a find operation, and then using the object to create the connection object using the basic implementation, the connection obtained through the DataSource object is the same as the connection obtained through the DriverManager facility.
DataSource's own approach
Connection getconnection () attempts to establish a connection to the data source represented by this DataSource object
Connection getconnection (String username,string Passward) attempts to establish a connection to the data source represented by this DataSource object
Inherited method Commondatasource Wrapper,commondatasource above has explained not to repeat, say Wrapper method
-
Boolean iswrappsefor (Class iface)
This article is from the "Grok World" blog, please be sure to keep this source http://7449328.blog.51cto.com/7439328/1909676
How to connect in db practice