当前位置:网站首页>MySQL_ JDBC
MySQL_ JDBC
2022-06-25 16:22:00 【weixin_ forty-eight million six hundred and forty-four thousand】
1. JDBC Introduce
We learned before MYSQL when , In order to use MYSQL service , We usually use the client and MYSQL Service to connect , Then you can enter SQL Statement for database
Various operations of . The client has command line and graphical interface 2 Kind of .
But in more circumstances , It's inefficient for us to manipulate data directly , Such as double 11 In this business scenario , Tens of millions or even hundreds of millions are often generated in the library in a second
Data , It is unrealistic to rely on people to operate by hand , Can only rely on the program for such highly concurrent operations .
There are many programming languages , such as Java、Python、C/C++ etc. , If the program language wants to execute SQL sentence , You must also connect to the database first , How many databases are there
Kind of , such as MySQL、Oracle、SQL Server etc. .
Different programming languages to connect to different databases , If there is no unified standard or specification , It must be quite chaotic .Java The language solution to this is
JDBC.
JDBC Defines a set of specifications and standards , It corresponds to various interfaces and abstract classes ( Usually corresponding java.sql Various classes and interfaces under the package ), The specific implementation is handed over to each data
Library manufacturers to complete ,MySQL Have their own implementation classes and make them jar Package release , For program developers ;Oracle It also has its own implementation jar package .
When we developers use it , According to the difference of connecting to the database , Go to the corresponding official website to download the database driver corresponding to the database version and program language (Java language
The word corresponds to a jar package ).( For example, we use MySQL 5.1, I'm going MySQL Download from the official website Java Language corresponding jar package )
JDBC : Java DataBase Connectivity (java Database link )
It's Jean java Linked database API
API : Application Programming Intergace ( Application program interface )
Is the function library
therefore JDBC Is to provide java The of the application program interface connecting to the database , Just interfaces or abstract classes
and JDBC Namely java A specification provided in , Basically, interfaces and abstract classes are used as parent classes , Concrete implementation , It was the database manufacturer who got it , But these manufacturers need to press
Implement according to my interface standard
If we want to operate the database , We need to put the implementation class developed by the manufacturer , Import in
2. JDBC Use steps
The first 0 Step : Guide pack
The first 1 Step : Registration drive ( Just once )
The first 2 Step : Establishing a connection (Connection)
The first 3 Step : Create run SQL Statement object (Statement)
The first 4 Step : Run statement
The first 5 Step : Process run results (ResultSet)
The first 6 Step : Release resources
package mian;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Jdbc_01 {
// The first 0 Step : Guide pack
// The first 1 Step : Registration drive ( Just once )
// The first 2 Step : Establishing a connection (Connection)
// The first 3 Step : Create run SQL Statement object (Statement)
// The first 4 Step : Run statement
// The first 5 Step : Process run results (ResultSet)
// The first 6 Step : Release resources
public static void main(String[] args) throws Exception {
// The first 1 Step : Registration drive ( Just once )
Class.forName("com.mysql.jdbc.Driver");
// The first 2 Step : Establishing a connection (Connection)\
Connection conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/_217_zuoye","root","224562");
// The first 3 Step : Create run SQL Statement object (Statement)
String sql="select * from test_jdbc";
Statement statement= conn.createStatement();
// The first 4 Step : Run statement
ResultSet resultSet = statement.executeQuery(sql);
// The first 5 Step : Process run results (ResultSet)
while(resultSet.next()){
System.out.print(resultSet.getString(1)+" ");
System.out.print(resultSet.getString(2)+" ");
System.out.print(resultSet.getString(3)+" ");
System.out.println();
//System.out.println(" Label printing "+resultSet.getString( "name"));
}
// The first 6 Step : Release resources
resultSet.close();
statement.close();
conn.close();
}
}
Business
What is business ?
In computer terminology, a program execution unit that accesses and may update various data items in a database (unit)
Four characteristics
Four characteristics :
ACID
Atomicity (atomicity). A transaction is an indivisible unit of work , The operations included in the transaction either do , Either not .
Uniformity (consistency). The transaction must be to change the database from one consistency state to another . Consistency is closely related to atomicity .
Isolation, (isolation). The execution of one transaction cannot be interfered by other transactions . That is, the operations and data used within a transaction are isolated from other concurrent transactions
Of , Transactions that execute concurrently cannot interfere with each other .
persistence (durability). Permanence is also called permanence (permanence), Once a transaction is committed , Its changes to the data in the database should be permanent
Of . Subsequent operations or failures should not have any impact on it .
Connection pool
The database connection pool is responsible for allocation 、 Manage and release database connections , It allows applications to reuse an existing database connection , Instead of rebuilding
individual ;
Release database connection with idle time exceeding the maximum idle time to avoid missing database connection caused by no free database connection .
This technology can obviously improve the performance of database operation
Create connection pool tool class
public class BasicDataSourceUtil {
private BasicDataSource bds;
private static BasicDataSourceUtil bdsu;
/**
* Put the code that initializes the database link , Put it in In the construction method of singleton pattern
*
* Because the current class is singleton , So the constructor can only be executed once , Then the link pool object becomes a singleton
*/
private BasicDataSourceUtil() {
bds = new BasicDataSource();
Properties properties = PropertiesUtil.getProperties();
bds.setDriverClassName(properties.getProperty("driver"));
bds.setUrl(properties.getProperty("url"));
bds.setUsername(properties.getProperty("username"));
bds.setPassword(properties.getProperty("password"));
}
/**
* Mainly to solve the problem of high concurrency , So you need thread safe singleton mode
*
* @return
*/
public static BasicDataSourceUtil getInstance() {
if (bdsu == null) {
synchronized (BasicDataSourceUtil.class) {
if (bdsu == null) {
bdsu = new BasicDataSourceUtil();
}
}
}
return bdsu;
}
/**
* Provide a method to get the linked object
*
* @return
*/
public BasicDataSource getBasicDataSource() {
return bds;
}
}
边栏推荐
- NFT元宇宙发展能做什么?
- Learning notes of rxjs takeuntil operator
- Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官
- Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
- Uncover gaussdb (for redis): comprehensive comparison of CODIS
- MySQL installation tutorial
- TensorFlow加载cifar10数据集
- 10 Super VIM plug-ins, I can't put them down
- Process control and method
- What is the NFT digital collection?
猜你喜欢
Mixed density network (MDN) for multiple regression explanation and code example

揭秘GaussDB(for Redis):全面对比Codis

Precautions for function default parameters (formal parameter angle)

10 Super VIM plug-ins, I can't put them down
Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
MySQL installation tutorial

Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's MAE

Educational administration system development (php+mysql)

加密潮流:时尚向元宇宙的进阶

Report on Hezhou air32f103cbt6 development board
随机推荐
Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution
Function and implementation of closures
leetcode-8. String to integer (ATOI)
Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
GridLayout evenly allocate space
Mt60b1g16hc-48b:a micron memory particles FBGA code d8bnk[easy to understand]
[issue 24] one year experience of golang to develop futu
Time wheel and implementation analysis of time wheel in go zero
MySQL installation tutorial
Prototype chain analysis
What can one line of code do?
Common APIs and exception mechanisms
元宇宙系统的概念解析
What plug-ins are available for vscade?
Beginner bug set
White screen, how fouc is formed, and how to avoid it
Classic deadlock scenario of multithreading and its solution (philosopher dining problem)
After flutter was upgraded from 2.2.3 to 2.5, the compilation of mixed projects became slower
Continuous integration of aspnetcore & cloud flow
一文带你搞懂 JWT 常见概念 & 优缺点