JDBC comprehensive example

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

JDBC comprehensive example

This comprehensive example mainly uses JDBC to add, delete, check, and modify database tables. These methods are also tested one by one, so test cases must be written. The main significance of writing this example is to practice and further familiarize yourself with the way JDBC adds, deletes, inquires, and modifies database tables, and consolidates the knowledge point of JDBC.
The technologies used in this example are: Maven, DBUnit, C3P0 connection pool, Log4j, MySQL database.
1. First prepare a clean database and two tables, and build an association relationship between these two tables:

image

image

2. Create a Maven project and configure all dependencies:

image

Configure the dependency package:

image

3. Create the corresponding package (package) in the source code folder and the test source code folder for classification, so that all classes are crowded together and appear messy:

image

Packages under the source code folder:
c3p0manager This package is used to put org.zero01.iuds of connection object management class
This package is used to put exception of encapsulation class and functional class
This package is used to put custom exception class

The package under the test source code folder:
org.zero01.test This package is used to put test case classes

4. Create a class under the c3p0manager package. This class is used to obtain database connection objects through the C3P0 connection pool, and this class is a singleton mode:
code example:

image

image

5. Write a data encapsulation class for two tables:
Cls code example:

image

Student code example:

image

image

6. Write a functional class that operates on the Cls table:

image

image

image

image

image

image

7. Write a functional class that operates on the Student table:

image

image

image

image

image

image

image

8. Write a test class in the org.zero01.test package under the test source code folder, and test the methods of the two functional classes one by one:

image

image

image

image

image

image

image

image

image

image

image

image

image

image

image

Test Results:

image

Test coverage:

image

Printing of log information:

image

Tip: This is the completed code, but when you actually write the code, it is best to write a method and test it. Don't write the test case test after writing all the functional code, because once there is a problem, many places If you write a method and test a method, you only need to modify one method, and other methods do not need to be changed, which is more efficient and easier to modify the code, no need to modify and modify everywhere. Be dazzled.

When I was writing the C3P0Manager class, I made a small logical error. I will record the error here. This error occurred because I declared the properties of the construction object in front of the variables of the C3P0 data source class, outside the constructor. When calling the getConnection method, a null pointer exception is thrown:

image

Diagram:

image

Correct way to declare:

image

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647691&siteId=291194637