JDBC (full name: Java data Base Connectivity) is a Java database connection short, providing the ability to connect to various databases
The main features of the JDBC API are:
- Establish a connection to the database
- Execute SQL statement
- Processing results
Use of the JDBC keyword:
- DriverManager: Depending on the database, the JDBC driver is managed
- Connection: Responsible for connecting the database and serving as the task of transferring the database
- Statement: Generated by connection, responsible for executing SQL statements
- ResultSet: Responsible for saving the results of the query resulting from statement execution
- PreparedStatement interface (Precompiled SQL statement) improves the performance of SQL statements, code security, code readability, and maintainability
Statement Common methods:
- ResultSet executeQuery (String sql): Execute SQL query and get ResultSet object
- Int executeupdate (String sql): You can perform inserts, deletes, updates, and so on, and the return value is the number of rows affected by performing the operation
- Boolean execute (String SQL): You can execute any SQL statement and then get a Boolean value that indicates whether to return resultset
- Boolean next (): Moves the cursor down one line from the current position
- Boolean Previous (): Cursor moves up one row from the current position
- Void Close (): Close ResultSet Object
- int getInt (int colindex): Gets the current row of the result set with the specified column number value in int form
- int getInt (String Collabel): Gets the column name value specified by the current row of the result set in int form
- float getfloat (int colindex): Gets the result set current row specified column number value in float form
- Float getfloat (String Collabel): Gets the current specified column name value for the result set in float form
- String getString (int colindex): Gets the current row specified column number value in sting form
- String getString (String Collabel): Gets the current row specified column name value in string form
PreparedStatement than statement improves the readability and maintainability of the code, improves the performance of SQL statement execution, and improves security
The steps of the JDBC operation:
- Loading the JDBC Driver
- Establish a connection to the database
- Create a statement or PreparedStatement object
- Send the SQL statement and get the result returned
- Handling returned results
- Freeing resources
Traversing result set data can use column numbers or column names to identify columns
Data access Layer-DAO mode
Persistence is the mechanism by which data in a program is transformed between transient state and persistent state.
main actions for persistence : Read, find, save, modify, delete
DAO (Data Access Object): Access to persisted data, between business logic and persisted data
DAO acts as a translator between entity classes and databases, and can convert entity classes to records in a database
The DAO pattern is the function:
1. Isolate business logic code and data access code
2. Isolate the implementation of different databases
Part of the DAO Pattern:
1.DAO interface
2.DAO Implementation Class
3. Entity classes
4. Database connection and Shutdown tool classes
Tiered development:
A software development method for small, divide and conquer
Features of layering:
1. Each floor has its own responsibilities
2. The upper layer does not care about the next implementation details, the upper layer through the external interface provided by the lower layer to use its function
3. The previous layer calls the next layer of functionality, the next layer cannot call the function of the previous layer
Benefits of tiered development:
1. Each layer focuses on the realization of its own function, which facilitates the improvement of quality
2. Facilitate the division of Labor collaboration, improve development efficiency
3. Easy code Reuse
4. Easy program Extension
Layering principle:
Encapsulation principle
Each level exposes the interface outward, but hides the internal details
Sequential access principles
The next tier serves the previous tier, but does not use the upper level services
In a hierarchical structure, data is transferred between different tiers through entity classes
To develop the DAO pattern according to the hierarchy create step:
Start the process:
1: Establish database epet, build table;
2: Create the entity class, and the corresponding database table is corresponding;
3: Create the DAO's base class interface class;
4: Create a DAO implementation class;
5: The DAO class that creates the concrete table;
6: The DAO implementation class that creates the concrete table;
7: Create an interface class for the business logic layer;
8: Create the interface implementation class of the business logic layer;
9: Create a test class;
Java about JDBC and DAO patterns using