MySQL (8)

MySQL (8)

Foreword:

Knowledge point review: We have learned about our transaction above, and we know that a transaction is to package independent operations into a whole, such as 1 + 1 and want to write this result on paper at this time, then 1 + 1 calculation It can be seen as an operation. Writing answer 2 on paper is also an operation. The transaction is to combine these two operations into a whole. At this time, if we want to only calculate 1 + 1 = 2 without writing it on paper, This is not possible. The entire process of calculation and writing must be completed. This is the atomicity of the transaction.

In addition: We also learned about some problems caused by concurrent execution of the database, such as dirty reads, non-repeatable reads, and phantom reads.

Dirty reading:

simple example: Zhang San passed and update user set math = 100 where name = '李四' ;changed Li Si's math score to 100.

At this time, Li Si select * from user where name = '李四' got and found that the math score was 100. He told the whole news to his classmates. Look at my math test It’s

not too bad, but after a while, Zhang San found that he misread 10 as 100, so he update user set math = 100 where name = '李四'changed it . At this time, the data that Li Si queried was dirty data. It is called dirty reading.

If Zhang San and Li Si can be regarded as transactions A and B, it can be concluded that while transaction A is modifying a certain data, transaction B reads the data. At this time, what transaction B read is likely to be a "dirty data" (this data is a temporary result, not the final result)


Non-repeatable read: In the same transaction, the same query gets different results at different times. For example, a transaction reads a row of data at T1, and when the row is read again at T2, the data of this row has been modified, so when it is read again, a different result is obtained from the query at T1.


To put it simply, Zhang Shan uses a SQL such as: select * from user where id = 2to query the data, at the beginning, he got the data through SQL, assuming that there is a field in the data called name, and now he gets a name of, and at this time there is another 张三person, Li Si, through update user set name = '李四' where id = 2 , here At that time, Li Si changed the entire information, and when Zhang San passed select * from user where id = 2the query the information obtained was different from the previous one.


Phantom reading: The same query gets different results at different times, which is the problem of phantom reading in transactions. For example, a SELECT that is executed twice, but the second time returns a row that was not returned the first time, is a "phantom" row.

To put it simply: Zhang Shan select * from user;finds a piece of data in the user table through , at this time Li Si insert into user values()inserts another piece of data into the user table through , and Zhang San finds out one more piece of data through select * from userthe query , then it becomes a phantom reading at this time question.


Replenish:

Let me add here: There are 4 transaction isolation levels in our MySQL: read uncommitted (dirty read/non-repeatable read/phantom read problems), read committed (non-repeatable read/phantom read problems), repeatable read (There is a phantom read problem) and serialization, where repeatable read is the default transaction isolation level of MySQL. Dirty reads read uncommitted data from other transactions, non-repeatable reads read data modified by other transactions, and phantom reads read "phantom" row data added or deleted by other transactions

After the review, let's enter our JDBC study:

JDBC

When we were learning MySQL before, we always typed SQL in the black box, but in the actual development, SQL is rarely entered manually. Most of the SQL is automatically executed through code, and other programming is required here. language to operate the database server.

insert image description here

Here we can implement our client through the API provided by MySQL.

Extension: The API originally provided by MySQL is in C language, but considering that MySQL is widely used, it also provides APIs in other languages. In addition, APIs in other languages ​​still use C language in essence. Operations invoked by the language.


piece of cake :

There are many databases on the market, such Oracle , SQLServer ,SQLiteas , which provide their own data API, so APIare these the same or do they each have their own system?

Answer: It is different here. Different databases here are developed by different people, and everyone will develop with their own ideas, so will these APIs be the same?


Export JDBC

After reading the small questions, have you ever thought that each of these many databases has its own set of APIs, assuming that we have mastered the APIs MySQL, and later when the company finds out that the databases used by the company are Oracle, then we still need to Oraclemaster the APIs, if Later, the job-hopping company uses other databases, so it needs to master other APIs, so it will be incompetent. I rely on me to learn wherever I go. At this time, are we eager for someone who can Stand up, unify these APIs into one set, make it a standard, and then we can learn one set.

Someone stood up in our java, and he is what we will learn next JDBC . Why is JDBCit because our JDBC API has become a part of the java standard library. Due to the great influence of java, As the standard itself, various database vendors provide JDBC-related driver packages (driver packages: equivalent to the specific implementation of the API, JDBC agrees on what the API has and how to use it)

With JDBC, we can only master this set of API, and then we can operate the database on the market (as long as it provides the java driver package).


Knowing the reason, here is a little look at the concept:

JDBC, namely Java Database Connectivity, java database connection. Is a Java API for executing SQL statements, which is a database connection specification in Java. This API is composed of some classes and interfaces in the java.sql. , javax.sql. packages. It provides a standard API for Java developers to operate databases, and can provide unified access for various relational databases.

After reading the concept, let's learn how to use JDBC

Use of JDBC


1. Install the driver package corresponding to the database.

I said before that JDBC unifies the APIs of various databases into one standard. We only need to master the JDBC``API 即可,但是有一个前提是需要让JDBC` that has the driver package provided by the database manufacturer, so if we want to use JDBC, then we can Need to install the driver package corresponding to our database.

insert image description here

Here we need to operate the driver package MySQLthat needs to be MySQLinstalled , where can I download it? We can go directly to the official website to download.

Digression: Because MySQL was acquired by Oracle, we need to go to Oracle's official website to download, but because Oracle's official website needs to register a bunch of things, it is not recommended to go to Oracle's official website to download, here you can go to our central The warehouse is used for downloading. Here, the central warehouse is equivalent to an application store, which contains libraries and components provided by various manufacturers.

The address provided here can be saved for a while, and will be used later: Maven Repository: Search/Browse/Explore (mvnrepository.com)

Click into this website:

insert image description here

Because we need the driver package, just MySQLsearch directly hereMySQL

insert image description here

Click on the first one:

insert image description here

Here we demonstrate version 5.1.49:

insert image description here

After downloading our driver package, you can see that it is a compressed package in .jarthe format of java, which is similar to a compressed package in the format of java .rar .zip. Here we can use the decompression tool to see

insert image description here

With .jarthe package , we can import it into the idea and use it.


open our idea

insert image description here


Now we can write our database code:

1. Create a data source operation


Figure 1:

insert image description here


Figure II:

insert image description here


Figure 3:

insert image description here


Figure 4:
insert image description here


At this point, our first step is completed. You can see that we only describe where the server is, and do not actually access it. The following connection operation menu is the real start of communication through the network.

2. Establish a network connection with the database

insert image description here

3. Construct SQL statement

insert image description here

4. Execute SQL + 5. Resource recovery

insert image description here

in addition:

The pools we have seen include string constant pools, and the database connection pools we just saw. Later, we can also see process pools, thread pools, memory pools, database connection pools, etc.

Then let’s have a brief understanding of Chi pool: Assumption: We are a girl who is good-looking and has many talented suitors. At this time, we can only talk about one object at the same time. At this time, we want to change another object. At this time The cost is relatively high, because it is necessary to re-cultivate the relationship with the new little brother. At this time, it is more troublesome. As the saying goes, does love grow after a long time? At this time, we thought of a way, which is to have an affair with B when talking with A ( This is essentially cultivating feelings), once we break up, can B immediately take over, and at this time, the efficiency is greatly improved, and B is called a spare tire at this time.

There is only one B here, assuming that we have CDEFG in addition to B and other ambiguities, will CDEFG form a spare tire pool at this time, so that we can change one every day, and it is very efficient at this time.

Note: The example here is not recommended in life. Although it is not recommended in life, it is very popular in computing. Here our database connection pool is like this. When we create a connection pool, we can prepare a batch of new connections. At this time It is not to establish one connection but to establish several connections. These connections are temporarily unused and placed in the pool and can be used at any time. If we need a lot of connections, we can directly take them out of the pool for connection use. In addition, the connections we release can also be placed in the pool.


Retrofit code:

Let’s continue: Our insert operation above is actually not very good, because we have written the data to death, insert into student values(1,'张三')here we have written the data we inserted to death, logically should let users add data by themselves, we according to the user’s The data is inserted, so the code here is a little unreasonable, let's modify it below.

Figure 1:
insert image description here

Figure II:

insert image description here

Here, our addition and deletion checks all return the number of affected rows. Finally, let's take a look at our search operation, which is also the key point (we will mostly search for data in the future), so let's learn about it here.

Find operation:

In fact, don’t panic too much, the search operation here is just one more step to traverse the result set

Figure 1:
insert image description here

Figure II:

insert image description here

Finally, let's write a query result by id

insert image description here

Guess you like

Origin blog.csdn.net/mu_tong_/article/details/127921647