Java JDBC based MySQL database connection function example explanation

This article describes the method of Java connecting to MySQL database based on JDBC. Share with you for your reference, as follows:

1、 Introduction to JDBC

Java database connectivity (JDBC) is an application program interface used in the Java language to regulate how client programs access the database. It provides methods such as querying and updating data in the database. JDBC is also a trademark of Sun Microsystems. JDBC is oriented to relational database.

1. JDBC architecture:

The JDBC API supports two-tier and three-tier processing models for database access, but the general JDBC architecture consists of two layers:

JDBC API: provides the application's management connection to JDBC;

Jdbc driver API: supports JDBC management to drive connection;

The use of JDBC API, driver manager and database specific driver provide transparent connection to heterogeneous databases;

Jdbc driver manager can ensure the correct driver to access each data source. The driver manager can support multiple concurrent drivers connected to multiple heterogeneous databases;

The following is a structure diagram showing the location of JDBC drivers and Java applications in the driver manager:

2. Common JDBC components:

The JDBC API provides the following interfaces and classes:

Drivermanager: this class manages the list of database drivers, whether the contents comply with the connection request of the correct database driver from the communication sub protocol used by the Java application, and identifies that the first driver of JDBC in a certain sub protocol will be used to establish database connection;

Driver: this interface handles communication with the database server and rarely directly with the driver object. On the contrary, it uses the object in the drivermanager to manage this type of object, and it also abstracts the detailed information related to the work of the driver object;

Connection: this interface is connected with all methods that contact the database. The connection object represents the communication context, that is, all communication with the database is through a unique connection object;

Statement: SQL statements of objects created using this interface can be submitted to the database. Some derived interfaces accept parameters other than executing stored procedures;

Resultset: after these objects are saved from the database, execute the SQL query using the statement object to retrieve data. It acts as an iterator so that you can move its data;

Sqlexception: this class handles any errors that occur in a database application

2、 Basic knowledge required to connect to JDBC

1. Basic operation of database,

Eg: installation and basic operations of MySQL (insert, delete, update, query)

2. The use of java development tools,

Eg: Eclipse / MyEclipse (including the import of mysql-connector-java-5.0.3-bin.jar)

3、 JDBC connection and code demonstration

1. JDBC connection tool class

1)、Configuration. Java: from The configuration information of connecting to the database in the XML file needs to be introduced into dom4j-1.6 1. Jar package

2)、db. XML: saves the configuration information of the database

3)、ConnectionFactory. Java: one of the JDBC connection factory methods

4)、ConnectionFactory2. Java: the second method of JDBC connection factory

5)、User. Java: bean class that defines the ID and name in the database table user, where the ID is automatically increased

6)、UserDao. Java: the operation class of user table, which implements insert, delete, update, query and other methods

2. Test class for JDBC connection

1)、TestJdbc. Java: database test class

3、 JDBC connection summary

The basic steps of JDBC operation are:

1. Create a connection object and pass in the SQL query command string; 2. Obtain Preparedstatement object: obtain by passing in SQL query command through connection object; 3. Get resultset: execute executeupdate() or executeqrey() on Preparedstatement; 4. Close the open objects successively: close the resultset, Preparedstatement and connection objects successively

Readers interested in more Java related content can view the special topics of this site: summary of java + MySQL database programming, tutorial on Java data structure and algorithm, summary of java file and directory operation skills, summary of Java DOM node operation skills and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>