JDBC Comprehensive Example

Source: Internet
Author: User

JDBC Comprehensive Example

This comprehensive example is mainly about using JDBC is the way to add, delete, check, and change database tables, which are also tested on a per-method basis, so you also write test cases. The main significance of writing this example is to practice and further familiarize yourself with jdbc 's Way of adding, deleting, checking, and modifying database tables to solidify This knowledge point.

The techniques to be used in this example are: Maven,DBUnit,c3p0 connection pool,log4j,MySQL Database.

1. prepare a clean database and two tables, and build an association relationship for the two tables:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/01dfbf308cfbc1cc7d7d1f9e51caf1d0.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 251.png "alt=" 01dfbf308cfbc1cc7d7d1f9e51caf1d0.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/e1cdded973140ee0607ccb705693fa34.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 253.png "alt=" E1cdded973140ee0607ccb705693fa34.png "/>



2. Create a Maven project and configure all of the dependent packages:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/7601bc4a759437ce4e341b5451510a49.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 278.png "alt=" 7601bc4a759437ce4e341b5451510a49.png "/>



Configure the Dependent packages:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/5155e91bb5d7392289fa41a9252fa987.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 289.png "alt=" 5155e91bb5d7392289fa41a9252fa987.png "/>



3. Create the appropriate package in the source folder and the test source folderto classify it so that all classes are cluttered together:

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/28/68e4b8d4d297d5d1ff80909552235ba3.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 348.png "alt=" 68e4b8d4d297d5d1ff80909552235ba3.png "/>


Packages under the source folder:

C3p0manager This package is used to put the Connection object management class

Org.zero01.iuds This package is used to encapsulate classes and function classes.

Exception This package is used to put the custom exception class

Test the package under the source folder:

Org.zero01.test This package is used to put the test case class

4. Create a class under the C3p0manager package that is used to obtain the database connection object through the c3p0 Connection pool, and this class is a singleton pattern:

code example:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/5b453cd763058549454c975d625bde91.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 554.png "alt=" 5b453cd763058549454c975d625bde91.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/aac03bb5d0897611f315673eefe7d832.png "title=" vip14-End Bowl Blowing water -9.10-JDBC Comprehensive example 556.png "alt=" Aac03bb5d0897611f315673eefe7d832.png "/>



5. Write data encapsulation classes for two tables:

Cls code example:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/973d7a079adc8926a36a33835d910be2.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 581.png "alt=" 973d7a079adc8926a36a33835d910be2.png "/>


Student code example:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/fba35ec68e4386ea6efa518fc0865e45.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 598.png "alt=" Fba35ec68e4386ea6efa518fc0865e45.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/7fccb6a409024d1e19e0f3758d605280.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 600.png "alt=" 7fccb6a409024d1e19e0f3758d605280.png "/>



6. write a functional class for CLS table operations:

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/28/1f5f19e6b2f8334305bf7aa68f155b6b.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 619.png "alt=" 1f5f19e6b2f8334305bf7aa68f155b6b.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/d7224ad7ca95593be9ab5c8e73156f27.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 621.png "alt=" D7224ad7ca95593be9ab5c8e73156f27.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/28/647aa4d9599b91a71278762163bb5a3f.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 623.png "alt=" 647aa4d9599b91a71278762163bb5a3f.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/28/892e8f1c483377ce63cf481648e419d0.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 625.png "alt=" 892e8f1c483377ce63cf481648e419d0.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/28/d003fe95da57c04c01da4de0831256ac.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 627.png "alt=" D003fe95da57c04c01da4de0831256ac.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/1171b02d53ecd27841d17655b058d8f5.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 629.png "alt=" 1171b02d53ecd27841d17655b058d8f5.png "/>




7. Write the function class for the Student table operation:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/39713567457b47833be20469ab21e81e.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 654.png "alt=" 39713567457b47833be20469ab21e81e.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/cc8cd63d5bbf22bc0c1928357aac210e.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 656.png "alt=" Cc8cd63d5bbf22bc0c1928357aac210e.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/fd4d62d1e3423e29a382fed39e6b391d.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 658.png "alt=" Fd4d62d1e3423e29a382fed39e6b391d.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/28/f789071322ae88d1b8c0e6007a812ada.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 660.png "alt=" F789071322ae88d1b8c0e6007a812ada.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/8c24f0d94103f343087f7d7833730932.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 662.png "alt=" 8c24f0d94103f343087f7d7833730932.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/28/5c1f9918b646cda219801e4fb0f8e4db.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 664.png "alt=" 5c1f9918b646cda219801e4fb0f8e4db.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/cbe629af6d7d0ab55ab5dec5cfecb708.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 666.png "alt=" Cbe629af6d7d0ab55ab5dec5cfecb708.png "/>




8. Write the test class in the Org.zero01.test package under the test source folder and test the methods of the two function classes individually:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/fcac3b6730ae4aa8ef52c015389885f1.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 720.png "alt=" Fcac3b6730ae4aa8ef52c015389885f1.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/b574f3febefd8dd10840bdee313ec364.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 722.png "alt=" B574f3febefd8dd10840bdee313ec364.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/8c4ee354dccf67bbb76e6e2165a98579.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 724.png "alt=" 8c4ee354dccf67bbb76e6e2165a98579.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/85b47aabd5cab3bab2d662461e795abb.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 726.png "alt=" 85b47aabd5cab3bab2d662461e795abb.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/28/61b6118439c36fb688db034e279ef785.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 728.png "alt=" 61b6118439c36fb688db034e279ef785.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/28/b1cc337869a603271cfda24ad7cb5474.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 730.png "alt=" B1cc337869a603271cfda24ad7cb5474.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/cf766c95c5196384c723c26dd508c81f.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 732.png "alt=" Cf766c95c5196384c723c26dd508c81f.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/0068f630eec9740186e68aa8bad70710.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 734.png "alt=" 0068f630eec9740186e68aa8bad70710.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/1c83ceeb2258a3f19276b87d6c205c31.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 736.png "alt=" 1c83ceeb2258a3f19276b87d6c205c31.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/28/4962006088fa5850074cb996c46652d8.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 738.png "alt=" 4962006088fa5850074cb996c46652d8.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/bcf7877f34ac0600a60ecc7346421e9d.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 740.png "alt=" Bcf7877f34ac0600a60ecc7346421e9d.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/eb79da91d2e39f8a7e44d57af085386d.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 742.png "alt=" Eb79da91d2e39f8a7e44d57af085386d.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/4953e8fdd50f5fe3a87a86e5ad934775.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 744.png "alt=" 4953e8fdd50f5fe3a87a86e5ad934775.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/17a043bf0adc958098c7f824c4979d81.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 746.png "alt=" 17a043bf0adc958098c7f824c4979d81.png "/>

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/28/e6e79c80791686e5442c286e5a980c80.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 748.png "alt=" E6e79c80791686e5442c286e5a980c80.png "/>



Test results:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/28/9dab0305c1f3b7e072dd101984cec572.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 758.png "alt=" 9dab0305c1f3b7e072dd101984cec572.png "/>



Test coverage:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/28/83f621b7d8a9e1dc8695a63c655eb3ed.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 878.png "alt=" 83f621b7d8a9e1dc8695a63c655eb3ed.png "/>



Print the log information:

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/28/9855455c2b96bdd4e92e0f86d214826d.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 891.png "alt=" 9855455c2b96bdd4e92e0f86d214826d.png "/>

Hint: I This is the finished code, but you actually write the code, it is best to write a method to test, do not write all the function code and then write test case test, because that if there is a problem, many places of code to be modified, if you write a method to test a method, You just need to make changes to one method, and other methods do not need to change, so it is more efficient and easy to modify the code, do not have to modify the changes everywhere to dazzle.


I was writing C3p0manager class, made a small logical mistake, here to record this error, this error occurs because I declare the properties of the build object in front of the c3p0 Data source class variable, in the constructor out of the getconnection method, a null pointer exception is thrown:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/75e530837ae0188a3b1f35ccfa99aa81.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 1175.png "alt=" 75e530837ae0188a3b1f35ccfa99aa81.png "/>



Graphic:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/841ca3e93e57d78ba8004ac39ab64c11.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 1182.png "alt=" 841ca3e93e57d78ba8004ac39ab64c11.png "/>



The correct way to declare:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/28/f65837639185c729a9bc07695f2f0ea7.png "style=" float: none; "title=" vip14-end bowl blowing water -9.10-JDBC Comprehensive example 1195.png "alt=" F65837639185c729a9bc07695f2f0ea7.png "/>


This article is from the "zero" blog, make sure to keep this source http://zero01.blog.51cto.com/12831981/1977020

JDBC Comprehensive Example

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.