API interpretation of the first--Establish a connection core object (JDBC)

Source: Internet
Author: User
Tags driver manager rollback savepoint

structure Diagram

Core ObjectsDriver

Java represents the driver through the driver interface, and each type of database provides its own driver implementation class by implementing the driver interface.

Driver consists of three parts: attribute, operation, and event.

Properties

Public properties

Version number: The version number consists of a two-field major version number (MajorVersion) and a secondary version number (minorversion).

Special Properties :

Driverpropertyinfo object: It has four fields, name represents the property name, value represents the property value, required. Indicates whether the attribute is required, description represents a description of the property.

Event

The Driveraction interface represents driver related events, which have only one method deregister, which has no parameters.

When the driver is registered to DriverManager, Driveraction is specified and the driver method of driveraction is called when DriverManager revokes deregeister. Examples such as the following

1 New tdriveraction ()); 2 drivermanager.deregisterdriver (driver);
Operation

Get Properties

    1. Getmajorversion: Gets the driver's major version number
    2. Getminorversion: Gets the driver's version number
    3. GetPropertyInfo (Url,prop): The parameter URL represents the address required to establish the connection, in the format jdbc:subprotocol:subname.

The JDBC representation is a fixed string that indicates compliance with the JDBC specification.

Subprotocol represents how a database is connected, such as the way an Oracle database Oracle:thin.

SubName represents the identity database, in the format @ip:port/dbname? Option IP is used to represent the IP address of the database server, port represents the port number, typically 1521,dbname represents the name of the database schema, and option is used to represent other parameters when connecting, such as encoding set, time-out, and so on.

Example: Jdbc:oracle:thin: @localhost: 1521/test.

The parameter prop represents the property at the time of the connection, such as the required attribute user name and password, or other optional parameters such as the encoding set. The method returns a type of Driverpropertyinfo array.

It is a friendly way to put all the parameters into prop, but it is not recommended to use them in the form of Key=value, which can be spliced into URLs.

Connection

    1. Accepturl (URL): A connection was established through this URL, and a successful return of true failed returns false.
    2. Jdbccompliant: If the connection follows the JDBC specification, the URL returns true at the beginning of JDBC, otherwise false.
    3. Connect (Url,prop): The parameter URL represents the connection address, and prop represents the connection property. Returns the Connection object
DriverManager

The Driver manager functions as a management driver, sets up logs, and establishes connections. All of its methods are static.

Management drivers

Management drivers include registration drivers, revocation drivers, and query registration success drivers.

Registered

Registerdriver (Driver): Parameter driver represents the driver class

Registerdriver (driver,driveraction): The parameter driver represents the driver class, driveraction represents the implementation class that implements the Driveraction interface, which has a method deregister method, when the drive is revoked , the method is called.

Revoke

Deregisterdriver (Driver): The parameter driver represents the driver class.

Inquire

Getdriver (URL): Based on the URL query driver, the parameter URL indicates the connection URL is established.

Getdrivers: All drivers are queried and the return value is enumeration<driver>.

Setting up logs

Set the log output stream, log the main record load driver, undo driver and other events.

    1. println (message): A message is printed to the log output stream, and the parameter message is of type Str.
    2. Setlogwriter (PrintWriter): Sets the log output stream to PrintWriter, and the parameter printwriter as an instance of the PrintWriter object.
    3. Getlogwriter (): Returns the log output stream.
Establish a connection

Driver Manager establishes a connection by invoking the connection method that it manages the drive object. It iterates through the drive collection, invokes the connection method of the drive object, and if a driver connection succeeds, stops the traversal and returns the connection object after the driver connection succeeds.

    1. getconnection (URL): The parameter URL is the URL that establishes the connection, and the necessary parameters are required on the URL.
    2. Getconnection (url,prop): url meaning ditto, prop as the properties object, encapsulating the necessary parameters.
    3. Getconnection (url,user,password): url meaning ibid., parameter user is username, password is password.
Setting the time-out period
    1. setLoginTimeout (Time): The parameter time is the timeout, in seconds. The default is 0, which means there is no limit.
    2. Getlogintimeout. Gets the time-out.
Connection

After the user connects to the database, the connection object is created. The Connection object represents a connection to the database. The connection session includes the SQL statement executed and the results returned through the connection. A single application can have one or more connections to a single database, or you can connect many different databases.

The role of the connection object is to obtain information, create an object that executes the SQL, control transactions, and data type mappings.

    1. Get information : This information mainly includes the information of the database, the information of the user. The DatabaseMetaData object is used to represent information about the database and is called by the Getdatabasemetadata method. The user information is obtained by calling the Getclientinfo method, and its return value is the Properties object.
    2. xxstatement Object : There are three types of objects that execute SQL, statement,preparedstatement,callablestatement.
    3. Transactions : The Connection object can get and set the isolation level of a transaction, can be archived for transactions (savepoint), commit transactions, and roll back transactions.
    4. Data Type Mapping : Establish a mapping relationship between the database data type and the Java data type, and the connection object is map<string,class<?>> to record the mappings between each other. Where the key value is the database data type,class<?> is the Java data type.
Create a Connection object

There are three ways to create connection objects, driver way, DriverManager way, DataSource way.

Kinds

    1. Connection: Normal Connection interface, each request needs to recreate the Connection object. For example, the connection object created by the drive mode
    2. Pooledconnection: A connection pool is established, each request is fetched connection object in the connection pool, and connection object is recycled after the request ends. Can only be created by DataSource.
    3. Xaconection: A Connection object that inherits from Pooledconnection and provides support for distributed applications. Can only be created by DataSource.
Get information

The connection object can obtain information about the database, information about the client.

DatabaseMetaData

Todo

Client

    1. Getclientinfo: Returns the client information, the return type is the Properties object
    2. Getclientinfo (name): Gets the value from the client information by name.
    3. Setclientinfo (Name,value): Sets the client information name to value.
    4. Setclientinfo (prop): The parameter prop is a properties object that adds a key-value pair from the Prop object to the client information.
Create a statement object

three ways to create statement :

Createstatement (resultsettype,resultsetconcurrency,resultsetholdability)

The parameter resulttype represents the traversal type of the result set.

      1. When Resultset.type_forward_only, indicates that the result set can only be traversed from the go, which is also the default way.
      2. When resultset.type_scroll_sensitive, these changes are synchronized to the result set when the database changes, and can be synchronized to the database when the result set changes.
      3. When Resultset.type_scroll_insensitive, indicates that the database change is not synchronized to the result set and vice versa.

The parameter resultsetconcurrency represents the read type of the result set:

      1. When Resultset.concur_updatable, indicates that the result set can be updated.
      2. Resultset.concur_readonly indicates that the result set is a read-only type, which is the default way.

The parameter resultsetholdability indicates whether the result set is closed with the commit of the transaction.

      1. Close_cursors_at_commit indicates that the result set is closed after the transaction is committed.
      2. Close_cursors_over_commit indicates that the result set is not closed with a transaction commit.

Createstatement (resultsettype,resultsetconcurrency): The parameter has the same meaning.

Createstatement: Creates the default Statement object, the default value for ResultsetType is Resultset.type_forward_only, The default value for resultSetConcurrency is resultset.concur_readonly.

six ways to create PreparedStatement :

    1. Preparestatement (SQL): Parameter SQL represents the SQL statement because there is a precompilation process, so SQL is the necessary parameter
    2. Preparestatement (sql,resultsettype,resultsetconcurrency): parameter meaning same as statement
    3. Preparestatement (sql,resultsettype,resultsetconcurrency,resultsetholdability): The parameter means the same as statement.
    4. Preparestatemtent (sql,autogeneratedkey): Parameter autogeneratedkey is valid only for INSERT statements, meaning Todo,statement.return_generated_ Keys indicates that the auto-generated primary key is returned, and Statement.no_generated_keys indicates that the auto-generated primary key is not returned.
    5. Preparestatement (sql,columnindex[]): The parameter columnindex represents the column index array, the type is an int array, and the column index starts at 1, exceeding the error.
    6. Preparestatement (sql,columnname[]): The parameter columnName represents an array of column names, and the type is a string array. The specified name does not exist and an error occurs.

Three ways to create CallableStatement

    1. Preparecall (SQL): parameter meaning is the same as PreparedStatement
    2. Preparecall (sql,resultsettype,resultsetconcurrency): The parameter means the same as PreparedStatement.
    3. Preparecall (sql,resultsettype,resultsetconcurrency,resultsetholdability): The parameter means the same as PreparedStatement.
Transaction

How to submit

    1. Getautocommit: Whether the transaction is autocommit, the default way to commit the transaction when the connection object is created.
    2. Setautocommit (Boolean): The value of the parameter is false, which means that autocommit is turned off, and that the parameter value is true in the form of autocommit.

Isolation level

    1. Gettransactionisolation: Returns the int value corresponding to the transaction isolation level.
    2. Settransactionisolation: The level of the parameter indicates the isolation of the transaction. There are five types of isolation levels for transactions
    • Transaction_none: Indicates that transactions are not supported. Value of 0,
    • Transaction_read_uncommited: Indicates that a transaction can read data that is not committed by another transaction. Dirty reads are present. Value of 1
    • Transaction_read_commited: Indicates that only committed data is read. Prevent dirty reads. There is repetition, phantom reading. Value of 2
    • Transaction_repeatable_read: Indicates that the modification operation is not allowed during the read process. Prevents a transaction from being queried multiple times for different values. Prevents dirty reads, reads repeatedly, and has virtual reads. Value of 4
    • Transaction_serializable:todo, the value is 8.

Dirty reads : There are two transactions, A/b. A transaction is modifying the data, the B transaction is reading the data, and the B transaction reads the data when it reads to a uncommitted transaction. For example, a transaction performed an update operation and was not committed, at which point a B transaction was read to these update operations.

Repeat read : The B transaction reads all the data that has been committed. During the B-transaction reading process, the A transaction modifies the same data, resulting in inconsistent results for each read in the B-transaction read process.

Phantom Read : TODO.

Archive

    1. Setsavepoint: Sets the archive point. Returns the SavePoint object
    2. Setsavepoint (name): The parameter name represents the name of the archive point and returns the SavePoint object.
    3. Rollback (savepoint): Rollback to savepoint archive, the parameter savepoint represents the SavePoint object.
    4. Releasesavepoint (savepoint): Removes the archive point in the current transaction.

SavePoint

    1. Getsavepointid: Gets the ID of the SavePoint object. Only the SavePoint object returned by the call to the Setsavepoint method has this value.
    2. Getsavepointname: Gets the name of the SavePoint object. Only the SavePoint object returned by the call to the Setsavepoint (name) method has this value.
Data mapping

Todo

DataSource

There are three types of data sources, Datasource,connectionpooldatasource,xadatasource,commondatasource, which provide a common method of data sources (set output stream and connection timeout).

Kind Commondatasource
    1. Getlogintime:
    2. Setlogintime (seconds): Sets the connection time-out, in seconds
    3. Getlogwriter: Get log output stream
    4. Setlogwriter (PrintWriter): Set the log output stream
    5. Getparentlogger: Returns the Logger object.
DataSource
    1. Getconnection
    2. Getconnection (User,password): The parameter user represents the username and password represents the password.
ConnectionPoolDataSource
    1. Getpooledconnection: The meaning is the same as the Getconnection method, the returned object is Pooledconnection
    2. Getpooledconnection (User,password).

Pooledconnection

    1. Addconnectioneventlistener (Connectioneventlistener): Add a connection listener, the listener has two methods connectionclosed, when the connection is closed trigger, Connectionerroroccured, the connection is triggered when an exception occurs.
    2. Removeconnectioneventlistener: Removing the connection listener
    3. Addstatementeventlistener (Statementeventlistener): Add statement Listener, Listener has two methods statementclosed,statement off when triggered, Statementerroroccured,statement triggered when an exception occurs.
    4. Removestatementeventlistener: Remove Statement Listener
    5. Getconnection: Return Connection object
    6. Close: Closes the connection.
Xadatasource
    1. Getxaconnection: The meaning is the same as the Getconnection method, and the returned object is xaconnection. A distributed connection cannot invoke methods such as Commit,rollback in a program. and set Autocommit to False.
    2. Getxaconnection (User,password)

Xaconnection

    1. Getxaresource: Returns the Xaresource object. Used to manage Xaconnection objects.

XAResource

Todo

API interpretation of the first--Establish a connection core object (JDBC)

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.