JDBC------Java technology to connect to the database

JDBC------Java technology to connect to the database

1. Definition
1. JDBC (Java Database Connectivity, JDBC for short) is an application program interface used in the Java language to regulate how client programs access the database, and provides methods such as querying and updating data in the database.
2. For any program, access to a specific database is actually inseparable from the local operation of the database engine.
3. The JDBC driver acts as a translator, translating and conveying the user's will to the underlying database engine. Perform
4, due to the difference in the underlying implementation of database products, different databases have different JDBC drivers. In order to facilitate management, JDBC provides a java.sql.Driver interface. Each database manufacturer implements this interface and provides drivers according to the characteristics of their own database products.
5. When writing a program, you only need to load the corresponding driver of the database and access various databases through a standard interface. If the database platform is changed, only the driver needs to be replaced, and other codes basically do not need to be modified.
2. JDBC driver type
Type 1: "JDBC-ODBC bridge driver", JDBC-ODBC bridge driver
Type 2: "native-API, partly Java driver driver, which converts JDBC calls into specific database calls.
Type 3:" JDBC-net pure Java driver ", it will call into JDBC for database-independent network protocol.
type 4:". native protocol, pure Java driver ", it converts JDBC calls to the database directly using network protocols
Insert picture description here
three , JDBC API
The JDBC API consists of two parts: the core Java API and the extended Java API. The core API is located in the java.sql package, including the basic Java data objects required to establish a DBMS connection and access DBMS data; the extended API is located in the javax.sql package, which is a part of J2EE, including some JDBC advanced features (such as database connection pool management ), and Java data objects that interact with JNDI.
The Java program uses the JDBC API to access the JDBC driver, and the JDBC driver translates these access messages into underlying messages that can be understood and processed by the DBMS, and completes the interaction
. Commonly used interfaces/classes of the Java.sql package are as follows:
1. DriverManager: According to Different databases, manage JDBC drivers.
The DriverManager class is used to obtain database connections. All its members are static members, so there is no need to instantiate it in the program, and you can access it directly through the class name.
The DriverManager class is the management layer of JDBC, which acts between the user's drivers.
2. Connection: Responsible for connecting to the database and as the task of transferring data.
The Connection object is an object that provides a connection between us and the data store, but this is not the full function of the Connection object. In addition to storing the details of the connection (such as the type of data storage and the features it supports), you can also use the Connection object to run commands.
3. Statement: Generated by Connection and responsible for executing SQL statements.
The Statement object is used to send SQL statements to the database. There are actually three kinds of Statement objects, which all serve as containers for executing SQL statements on a given connection: Statement, PreparedStatement (inherited Statement) and CallableStatement (inherited PreparedStatement). They are all dedicated to sending specific types of SQL statements:
Statement objects are used to execute simple SQL statements without parameters;
PreparedStatement objects are used to execute pre-compiled SQL statements with or without IN parameters;
CallableStatement objects are used to execute stored procedures on the database Call.
4. ResultSet: Responsible for saving the query results generated after the Statement is executed.
Load the query results. The ResultSet contains all rows that meet the conditions in the SQL statement, and it provides access to the data in these rows through a set of get methods (these get methods can access different columns in the current row). The record is a two-dimensional table with column headings and corresponding values ​​returned by the query. Similar to Figure
Insert picture description here
5 below,
when the SQL statement will be run many times, the PreparedStatement object has better performance (SQL statements can be pre-compiled, after the first execution, subsequent executions do not need to be recompiled and the Statement object is used to run the same statement multiple times This method is more efficient than the statement (which must be compiled every time the statement is run).
The SQL statement contained in the PreparedStatement object may have one or more IN parameters.
6, SQLException The
SQLException class is a child of the Exception class Class, it provides information about database access errors or other errors.
Fourth, use direct connection to operate the database
1. Load the database driver
2. Get connection
3. Use Connection to create PreparedStatement statement object
4. Use Preparedstatement object to execute SQL statement
5. Operate the result set ResultSet
6. Recycle resources
5. Summarize
JDBC API provides an interface for database operations
Using direct connection will improve the performance of database access.
PreparedStatement is a pre-compiled statement.
JDBC supports transactions, batch processing, and stored procedures.
Property files can be used to isolate java code and database configuration.
VI. Example display:
Use JDBC to connect to the database in Java.

Guess you like

Origin blog.csdn.net/tan1024/article/details/110876087