当前位置:网站首页>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;
	}
}

原网站

版权声明
本文为[weixin_ forty-eight million six hundred and forty-four thousand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190534173771.html