当前位置:网站首页>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;
}
}
边栏推荐
- What exactly is a handler
- 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
- Collection overview, array encapsulation
- What is the NFT digital collection?
- Error: homebrew core is a shallow clone
- SQL最常用的语句
- mysql整体架构和语句的执行流程
- Don't underestimate the integral mall, its role can be great!
- XML usage and parsing of data storage and transmission files
- Classic deadlock scenario of multithreading and its solution (philosopher dining problem)
猜你喜欢

不要小看了积分商城,它的作用可以很大!

Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected

20省市公布元宇宙路线图

Principle analysis of ThreadLocal source code

Beginner bug set
Mixed density network (MDN) for multiple regression explanation and code example
Classic deadlock scenario of multithreading and its solution (philosopher dining problem)
Power representation in go language

Understand the execution sequence of try catch finally in one diagram

Nsurlsession learning notes (III) download task
随机推荐
What are some tricks that novice programmers don't know?
Servlet详解
元宇宙系统的概念解析
Flutter assembly
flutter
绕过技术聊'跨端'......
Time wheel and implementation analysis of time wheel in go zero
[problem solving] dialogfragment can not be attached to a container view
Swift responsive programming
10款超牛Vim插件,爱不释手了
Reverse series to obtain any wechat applet code
20省市公布元宇宙路线图
Sleep formula: how to cure bad sleep?
Once the code was encrypted by the company's computer, the compilation failed
mysql整体架构和语句的执行流程
Go language - lock operation
Several ways of SQL optimization
加载本地cifar10 数据集
Why does golang's modification of slice data affect the data of other slices?
leetcode-8. String to integer (ATOI)