Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

👨🎓 author: Java Academic Conference

🏦 Storage: Github, Gitee

✏️ blogs: CSDN, Nuggets, InfoQ, Cloud + Community

💌 public account: Java Academic Party

🚫 special statement: the original is not easy, shall not be reproduced or copied without authorization, if you need to reproduce can contact xiaobian authorization.

🙏 Copyright notice: part of the text or pictures in the article come from the Internet and Baidu Encyclopedia, if there is infringement, please contact xiaobian as soon as possible. Wechat search public Java academic party contact xiaobian.

☠️ Hold on to your dreams and fly without wings.

👋 Hello! I’m your old friend Java Academic Party. Today I would like to share with you some techniques about database connection, which are often seen in development today. For example, JDBC and C3P0 technologies.

Java Database Connectivity (JDBC) is a common interface (set of apis) that is independent of a specific Database management system. It defines a standard Java class library for accessing databases. (java.sql.javax.sql) These libraries provide easy access to database resources in a standard way.

1.1 What is JDBC

Java DataBase Connectivity (Java language to connect to databases)

1.2 What is the nature of JDBC

JDBC is a set of interface (interface) developed by SUN Company java.sql*; (There are many interfaces in this package.)

Interfaces have callers and implementers.

Calling to the interface and writing implementation classes to the interface are all part of interface oriented programming.

1.3 Why face interface programming?

Decoupling: reduce the coupling degree of the program, improve the expansion force of the program.

Polymorphic mechanisms are typical: abstract-oriented programming. (Don’t program specifically)

Advice:

Animal a = new Cat();

Animal b = new Dog();
Copy the code

Feeding method:

public void feed(Animal d){
    // Programming to the parent class
        }
Copy the code

/ / do not recommend

Dog a = new Dog();
Cat c = new Cat();
Copy the code

1.4 Why did SUN develop a JDBC interface

1.5 What is the nature of JDBC?

A set of interfaces

6 Steps to JDBC Programming (Need to memorize)

  • Step 1: Register the driver (to tell the Java program which database to connect to)
  • Step 2: Get the connection (indicates that the channel between the JVM process and the database process is open. This is process to process communication, heavyweight. Always turn it off after use)
  • Step 3: Get the database action object (the object that specifically executes SQL statements)
  • Step 4: Execute SQL statements (DQL, DML…)
  • Step 5: Process the query result set (only when step 4 is a SELECT statement, only have these five steps to process the query result set)
  • Step 6: Release resources (Always close resources when you are done using them. Java and database are interprocess communication, must be turned off after enabled.

Url: Uniform resource locator (the absolute path to a resource in the network)

www.baidu.com/ is a URL

Which parts of the URL include:

  • agreement
  • IP
  • PORT
  • Resource name

http://182.61.200.7:80/index.html

http://communication protocol 182.61.200.7 Server IP address 80 Software port on the server index. HTML Name of a resource on the server

Connect the mysql database url of JDBC: mysql: / / 192.168.1.106:3306 / bjpowernode

Url for connecting to the oracle database JDBC :oracle:thin:@localhost:1521:orcl //orcl indicates the SID of the database JDBC :mysql:// protocol 192.168.1.106 indicates the IP address of the localhost (127.0.0.1 or localhost) 3306 Database port number bjPowernode Specifies the database instance name

Note: Localhost and 127.0.0.1 are both local IP addresses

Key: The principle of using DQL statements to process query sets

Note: The cursor starts at 0 with no data, and next determines if there is any data in the next row of the database.

Database connection pool (C3P0, Druid)

Concept: a container (collection) that holds database connections. When the system is initialized, the container is created, and some connection objects are applied for in the container. When the user accesses the database, the connection objects are obtained from the container. After the user accesses the database, the connection objects are returned to the container.

Advantages:

  • Saving resource
  • User Access efficiency

Implementation:

Standard interface: in the DataSorce javax. SQL package

  1. Methods:

    • Get the connection: getConnection()
    • Return method: connection.close (). If the Connection object Connection is fetched from the Connection pool, then the connection.close () method is called and the Connection is not closed. We return the connection.
  2. Generally, we do not implement this database connection pool, but the database vendor implements it.

    • C3P0: Database connection pooling technology
    • Druid: Database connection technology provided by Alibaba

C3P0 connection pool

Implementation steps:

  • Jar: c3P0-0.9.5.2. jar McHange-commons-java-0.2.12.jar Do not forget to import the database driver JAR package.

  • Defining a configuration file:

    • Properties or c3P0-config.xml.
    • Path: Directly save the file to the SRC directory.
  • Create core object: Database connection pool object (CombopooledDataSourc e)

  • Get the connection: getConnection()

——-💘 after watching the big guys can pay attention to the xiaobian, will always update the small skills, free to share with you!! 💝 — — — — —