1. Introduction to JDBC

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) Use these libraries to easily access database resources in a standard way.

JDBC provides a unified way to access different databases, shielding developers from some of the details.

The goal of JDBC is to enable Java programmers to use JDBC to connect to any database system that provides a JDBC driver, which greatly simplifies and speeds up the development process by eliminating the need for programmers to know too much about the characteristics of a particular database system.

2. Use JDBC to connect to the database

A:

Connect to MySQL5.6 using the API through the display

public class M1 {
    public static void main(String[] args) {
        try {
            //1. An object that provides the java.sql.Driver interface implementation class
            Driver driver = null;
            driver = new com.mysql.jdbc.Driver();

            //2. Provide the URL to specify the specific operation data
            String url = "JDBC: mysql: / / localhost: 3306 / corresponding database name";

            //3. Provide the Properties object, specifying the user name and password
            Properties info = new Properties();
            info.setProperty("user"."root");
            info.setProperty("password"."Your password");

            //4. Call connect() on the driver to obtain the connection
            Connection conn = driver.connect(url, info);
            System.out.println(conn);
        } catch(SQLException throwables) { throwables.printStackTrace(); }}}Copy the code

Method 2:

Note: In contrast to method 1, reflection instantiation Driver is used here, and third-party database API is not reflected in the code.

public class M2 {
    public static void main(String[] args) {
        try {
            1. Instantiate the Driver
            String className = "com.mysql.cj.jdbc.Driver";
            Class clazz = Class.forName(className);
            Driver driver = (Driver) clazz.newInstance();

            //2. Provide the URL to specify the specific operation data
            String url = "JDBC: mysql: / / localhost: 3306 / corresponding database name? useSSL=false&serverTimezone=UTC";

            //3. Provide the Properties object, specifying the user name and password
            Properties info = new Properties();
            info.setProperty("user"."root");
            info.setProperty("password"."Your password");

            //4. Call connect() on the driver to obtain the connection
            Connection conn = driver.connect(url, info);
            System.out.println(conn);
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

Three:

Use DriverManager to connect the database. Experience the four basic elements necessary for obtaining connections.

public class M3 {
    public static void main(String[] args) {
        try {
            //1. The four basic elements of database connection:
            String url = "JDBC: mysql: / / localhost: 3306 / corresponding database name? useSSL=false&serverTimezone=UTC";
            String user = "root";
            String password = "Your password";
            String driverName = "com.mysql.cj.jdbc.Driver";

            //2. Instantiate Driver
            Class clazz = Class.forName(driverName);
            Driver driver = (Driver) clazz.newInstance();

            //3. Register the driver
            DriverManager.registerDriver(driver);

            //4. Get the connection
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

Method 4:

Note: There is no need to explicitly register the driver. Because static code blocks already exist in the DriverManager source code to implement driver registration.

public class M4 {
    public static void main(String[] args) {
        try {
            //1. The four basic elements of database connection:
            String url = "JDBC: mysql: / / localhost: 3306 / corresponding database name? useSSL=false&serverTimezone=UTC";
            String user = "root";
            String password = "Your password";
            String driverName = "com.mysql.cj.jdbc.Driver";
            //2. Load Driver (① instantiate Driver ② register Driver)
            Class.forName(driverName);
            //Driver driver = (Driver) clazz.newInstance();


            //3. Register the driver
            //DriverManager.registerDriver(driver);

            Can / * comment out the reason of the above code, because the mysql Driver class declarations are: static {try {DriverManager. RegisterDriver (new Driver ()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!" ); }} * /


            //3. Obtain the connection
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
        } catch(Exception e) { e.printStackTrace(); }}}Copy the code

Method 5:

Use the configuration file to save the configuration information, load the configuration file in the code

Benefits of using profiles:

The separation of code and data is realized. If you need to modify the configuration information, you can modify it directly in the configuration file without going into the code

(2) If the configuration information is modified, the recompilation process is omitted.

Create jdbc.properties in the SRC directory of your project

User =root password= your password//localhost:3306/ Corresponding database name? useSSL=false&serverTimezone=UTC
driverClass=com.mysql.cj.jdbc.Driver

Copy the code
public class M5 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        //1. Load the configuration file and read the file using THE I/O stream
        InputStream is = M5.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);


        //2. Read configuration information
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");


        //3. Load the driver
        Class.forName(driverClass);


        //4. Get the connectionConnection conn = DriverManager.getConnection(url,user,password); System.out.println(conn); }}Copy the code