Hand-in-hand to do a JSP introductory program (3): DBHelper class design -- connect to mysql database

Keywords: Database MySQL JDBC Java

DBHelper class design - Connect to mysql database

_DBHelper (DataBase Helper) is mainly used to help us operate the database, in which we will create a method to obtain database connections. We'll put it in the util ity package, which acts as a toolkit.
In order to access MySQL database, we need a jar package to connect mysql: mysql-connector-java-5.0.5-bin.jar. click here Download. You can download it and put it in the lib directory of WEB-INF.

package util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBHelper {
//  Database Driver
    private static final String driver = "com.mysql.jdbc.Driver";
//  The URL address to connect to the database, using mysql's default port 3306
    private static final String url = "jdbc:mysql://localhost:3306/simpleshop";
//  Database login information, I do not use root users here, generally with a project, we can create an independent user with partial authority, root users are generally only used to manage the database.
    private static final String username = "superboy";
    private static final String password = "iamsuperboy";

    private static Connection conn = null;

//  Static code blocks are responsible for loading drivers
//  In general, if some code must be executed at the start of the project, it needs to use static code blocks, which are executed actively and take precedence over the main function.
    static{
        try{
            Class.forName(driver);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public static Connection getConnection() throws Exception{
        if(conn == null){
            conn = DriverManager.getConnection(url,username,password);
            return conn;
        }
        return conn;
    }
}

After writing, in order to confirm whether the connection can be normal, we can write a main function like this to test.

public static void main(String[] args) {
    try{
        //Get a connection
        Connection conn = DBHelper.getConnection();
        if(conn!=null){
            System.out.println("The database connection is normal!");
        }else{
            System.out.println("Exceptional database connection!");
        }
    }catch(Exception ex){
        ex.printStackTrace();
    }
}

On Class.forName
_When loading database drivers, we usually use Class.forName for loading. The role of Class.forName(xxx.xx.xx) is to require the JVM to find and load the specified class, that is to say, the JVM will execute the static code segment of the class. Therefore, if there is a static initializer in the class, the JVM will certainly execute the static code segment of the class. The JDBC specification explicitly requires that this Driver class must register itself with Driver Manager, that is, the code of any Driver class of JDBC Driver must be similar to the following. Now that we have registered in the static initializer, we only need Class.forName (xx xx. xx. xx); to use JDBC. Here XX xx. xxx. XX points to a class.

public classMyJDBCDriver implements Driver {
    static{
        DriverManager.registerDriver(new MyJDBCDriver());
    }
} 

Therefore, the static code block is used to load the driver, which can be loaded into the database driver at the beginning of the program execution, and only executed once. It is worth mentioning that the DBHelper class here is a Singleton mode . One of the advantages of this model is that it can control the number of instances and save system resources.

Posted by Sprout on Fri, 29 Mar 2019 13:48:27 -0700