Java JDBC basic learning summary

JDBC is a Java application program interface that encapsulates various operations on the database. JDBC consists of classes and interfaces. Developing database applications using java requires four main interfaces: driver, connection, statement and resultset. These interfaces define the general architecture of using SQL to access the database.   

1. JDBC related classes and APIs

JDBC is a Java application program interface that encapsulates various operations on the database. JDBC consists of classes and interfaces. Developing database applications using java requires four main interfaces: driver, connection, statement and resultset. These interfaces define the general architecture of using SQL to access the database.

Driver interface is an interface that all JDBC programs must implement. This interface is specially provided for database manufacturers. When writing a JDBC program, you must make a database driver or class library to load into the project classpath.

The drivermanager is used to load the jdbc driver and create a connection to the database. Two important static methods are defined:

Connection represents the connection to the database, which defines a series of methods:

The statement interface is used to send SQL statements to the database.

2. Program example

Database IP 192.168 1.150, the database name is test, the data table name is penetration, and the table structure is:

JDBC connection MySQL program example

The running result of the program is:

JDBC uses Preparedstatement for data insertion operation program example

Program output is:

@H_ 398_ 403@

3. Summary

When the program remotely accesses mysql, you cannot log in with the root account and password. For the sake of MySQL security, you can create a new MySQL account for testing.

If the remote MySQL host is centos7, you need to turn off the firewall temporarily and use the command systemctl stop firewalld service。

JDBC is a set of interfaces for operating database, which is equivalent to the abstraction layer of database. We can use it to operate common databases and SQL operations. When we write a program, for a table (user), we can create its corresponding class (user), which provides various get / set methods; Then create a new database operation tool class (JDBC utils), which provides methods to establish and close the connection with the database; Finally, create a new table operation class (userdao), which encapsulates the operations of adding, deleting, querying and updating the table user.

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
分享
二维码
< <上一篇
下一篇>>