Java about JDBC and DAO patterns using

Source: Internet
Author: User

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:

    1. Establish a connection to the database
    2. Execute SQL statement
    3. Processing results

Use of the JDBC keyword:

    1. DriverManager: Depending on the database, the JDBC driver is managed
    2. Connection: Responsible for connecting the database and serving as the task of transferring the database
    3. Statement: Generated by connection, responsible for executing SQL statements
    4. ResultSet: Responsible for saving the results of the query resulting from statement execution
    5. PreparedStatement interface (Precompiled SQL statement) improves the performance of SQL statements, code security, code readability, and maintainability

Statement Common methods:

    1. ResultSet executeQuery (String sql): Execute SQL query and get ResultSet object
    2. 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
    3. Boolean execute (String SQL): You can execute any SQL statement and then get a Boolean value that indicates whether to return resultset
    4. Boolean next (): Moves the cursor down one line from the current position
    5. Boolean Previous (): Cursor moves up one row from the current position
    6. Void Close (): Close ResultSet Object
    7. int getInt (int colindex): Gets the current row of the result set with the specified column number value in int form
    8. int getInt (String Collabel): Gets the column name value specified by the current row of the result set in int form
    9. float getfloat (int colindex): Gets the result set current row specified column number value in float form
    10. Float getfloat (String Collabel): Gets the current specified column name value for the result set in float form
    11. String getString (int colindex): Gets the current row specified column number value in sting form
    12. 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:

    1. Loading the JDBC Driver
    2. Establish a connection to the database
    3. Create a statement or PreparedStatement object
    4. Send the SQL statement and get the result returned
    5. Handling returned results
    6. 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

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.