1. Functions of JDBC
JDBC is short for Java DataBase Connectivity and is a standard API for various Java applications to connect to various DataBase management systems. Each database management system provides its own JDBC driver through which developers can interact with the database.
2. What information do I need to know when developing applications using JDBC?
First, you must have a driver that knows the driver name, URL format, and basic database information: IP address, port, database, user name, and password.
3. JDBC APIs
Driver: establishes a connection with the database.
DriverManager: Manage drivers
Connection: indicates a session with the database. No matter what operation is performed, a Connection must be established first.
Statement: used to execute SQL statements
ResultSet: indicates the query result set.
DataSource: data source, used to obtain connections in the connection pool
4. Basic database connection process
Load driver
Create a connection object
Create statement object
Write SQL statements
Execute SQL statements using statement objects
If a result set is used to process the result set
Close the result set object (if any), close the statement object, and close the connection object.
5. How to load the driver
Class. forName ("driver name "):
6. How to create a connection
The getConnection method of DriverManager has three parameters: the first parameter is the URL, the second parameter is the user name, and the third parameter is the password.
7. How to Create statement objects
Create a Statement object: con. createSteatement ()
Create a PrepareStatement object: con. prepareStatement (SQL statement );
Create a CallableStatement object: con. prepardCall (stored procedure name)
8. How to execute SQL statements
Execute the select statement: the executeQuery method of the statement object
Execute the insert delete update statement: the executeUpdate method of the statement object
9. When the result set is obtained, the Pointer Points to the front of the first record. If one record exists, use if (rs. next (). if multiple records exist, use while (rs. next ())
10. How to obtain a column in the result set?
Using methods such as rs. getString, the method parameter specifies the column sequence number or column name.
11. How do I close objects?
Close the result set, close the statement object, and close the connection object.
12. How to Use JDBC for transaction processing?
Con. setAutoCommit (false );
Execute SQL statements
Con. commit submit