How to use JDBC to connect to Mysql database

//java class name BaseDao
public class BaseDao {
    private Connection conn = null; // Declare the Connection object, the Connection interface is used to establish a connection with a specific database, private private can only be called in this class

    // Constructor to connect to the database, 1 Load the driver, 2 establish a connection, 3 use the sql statement for database operations, 4 release resources.
    // (1) The name of the constructor must be exactly the same as the name of the class in which it is defined, and there is no return type, not even void; //
    // (2) The constructor is called using the new operation when creating an object of. The role of the constructor is to initialize the object.
    // (3) Each class can have zero or more constructors; (4) cannot be modified by static, final, synchronized, abstract and native. Constructors cannot be inherited by subclasses.
    // (5) The constructor is automatically executed when the object is created, and generally cannot be called directly and explicitly.
    public BaseDao() {
        String driver = "com.mysql.jdbc.Driver"; // The mysql database driver, here can be replaced with other database drivers you want
        // try { // The code to execute, there may be exceptions . Once an exception is found, it immediately jumps to catch execution. Otherwise, the content in the catch will not be executed}
        try {
            /*
             * The role of the Class.forName method is to initialize the given class.
             * In our given MySQL Driver class, it registers the driver through the JDBC DriverManager in the static code block.
             * We can also directly use the JDBC Driver Manager to register the mysql driver. Instead of using Class.forName.
             * /
            Class.forName(driver); // Load the driver
            // The specified database connection URL address, database name, encoding format
            String url = "jdbc:mysql://localhost:3306/dev?characterEncoding=UTF-8";
            conn = DriverManager.getConnection(url, "root", "");// Get the connection object, address, username, password
            System.out.println("connected to the mysql database successfully");// console output
            // catch { // The code here will not execute unless an exception occurs in the code executing inside the try }
        } catch (Exception e) {
            // This will catch any exceptions that occur. In addition, the e parameter is also provided, which can be used to obtain information about the exception when handling the exception.
            e.printStackTrace();
        } finally {// finally { //It will be executed no matter what, including the use of return in try catch, it can be understood that as long as try or catch is executed, it will be executed
                    // finally }
            // Release resources, conn.close;
        }

    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324767764&siteId=291194637