[MySQL] Summary of key knowledge of JDBC programming

1. JDBC (API):

  1. I have learned a lot of SQL before, but most of the SQL in actual development is not typed by hand, but is executed through programs.

  2. Various databases will provide API to facilitate programming language to control; API (Application Programming Interface application programming interface, provides a set of classes/methods, allowing programmers to call directly)

  3. Different types of databases provide different APIs. In order to reduce the learning cost of programmers, Java就提供了一组统一风格的数据库操作API然后让这些数据库厂商适配Java的APIprogrammers only need to master a set of Java API (JDBC) to operate various databases.

  4. insert image description here

  5. Since there is a need 驱动包进行接口之间的转换, there is a need 先把驱动包给下载下来, 并且导入到项目中.

  6. How to download the driver package? Search MySQL in the central warehouse, the first result mysql-connector-javais the driver package you need to find. The version of this driver package must match the major version of the MySQL server

  7. The downloaded driver package file is a file with a .jar suffix, which is called a jar package. A jar package is essentially a compressed file. 里面很多 .class文件(a binary file generated by compiling .java). jar包其实是一个java程序发布的一种常见手段, There may be many .class files in a program, so they are packaged into A compressed package is much more convenient.

  8. Environment: When using JDBC, you need to download and import the driver package of the corresponding database. Import the downloaded jar package into the project for use:insert image description here

2. JDBC code writing:

2.1 Create a data source object:

  1. DataSource是个接口, using polymorphism here is a way to reduce coupling.

  2. The data source object describes the要访问的数据库是啥, 在哪.insert image description here

  3. Each database will provide corresponding classes to implement the DataSource interface. DataSource serves various databases, and MysqlDataSource only serves MySQL.

  4. insert image description here

  5. DataSource built-in connection pool, connection reuse, so as to improve efficiency. (After resources are ready, use them at any time, take them at any time)

2.2 Establish a connection:

  1. Before accessing data, a connection must be established with the database server.先建立连接再进行后续的操作.insert image description here

  2. Use Connection to represent the connection, 应该使用JDBC里面的Connectioninstead of the Connection in the MySQL driver package.

  3. MySQL的客户端和服务器在一个主机上, 是通过环回IP来连接的, this has nothing to do with connecting to the outside network.

2.3 Construct SQL statement:

  1. First describe the SQL to be executed through a string, and then construct a PreparedStatement object.insert image description here

  2. The SQL statement is sent to the database, and the database has to analyze the SQL. This process of analysis is actually time-consuming and resource-consuming. At the same time, the database also needs to process many client requests, and all the analysis work is done by the server. Completed, this makes the system resources that are not rich in the first place worse.

  3. In order to solve the above problems, PreparedStatement needs to be used. 把一部分的解析工作交给客户端来完成After the client parses, the result is sent to the database, which saves resource overhead and improves processing efficiency.

  4. Recommended action
    :insert image description here


insert image description here

The above code is through string splicing, which allows users to input data for insertion. Although the problem is solved, it is not very good!

1. The code is very messy

2. Insecure, easy to cause SQL injection vulnerabilities

2.4 Execute SQL:

  1. The method of executing the code here to modify and query calls is different.

  2. insert image description here

  3. insert image description here

  4. insert image description here

2.5 Release resources:

  1. 释放的顺序要和申请的顺序正好相反, the one created first is released, and the one created later is released first.insert image description here

  2. The resources that are often mentioned generally refer to some hardware resources (CPU/memory/hard disk/network card bandwidth). What is released here is memory resources.

Guess you like

Origin blog.csdn.net/qq_68993495/article/details/128390585