This paper analyzes The solutions and causes of “java.sqL.sqlexception: The URL Cannot be NULL” when using JDBC tools, involving The execution sequence of static variables and static code blocks, and The execution sequence of class loading instantiated objects

1. The solution

1.1 Attaching source code

JDBCUtils.java

package com.jdbc.day0511.word;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/ / JDBC tools
public class JDBCUtils {
    // Define the connection database properties
    private static String url = null;
    private static String user = null;
    private static String password = null;
    private static String driver = null;

    // Read configuration file, static, only need one time to get the connection required properties
    static{
        try {
            // Use the classloader to get the current classpath
            // Get the classloader object
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            // Find the resource with the specified name through the class loader's getResource()
            URL resource = classLoader.getResource("jdbc.properties");
            String path = resource.getPath();
            // Load the file
            Properties pro = new Properties();
            pro.load(new FileReader(path));
            // Get values based on keys
            String url = pro.getProperty("url");
            String user = pro.getProperty("user");
            String password = pro.getProperty("password");
            String driver = pro.getProperty("driver");
            // Register the driver and the class loading will take place
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch(ClassNotFoundException e) { e.printStackTrace(); }}// Define the method to get the database Connection object Connection
    public static Connection getConnection(a) throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }

    // Release resources
    public static void close(ResultSet rs, Statement stat, Connection conn){
        if(rs ! =null) {try {
                rs.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}if(stat ! =null) {try {
                stat.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}if(conn ! =null) {try {
                conn.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}}public static void close(Statement stat, Connection conn){
        if(stat ! =null) {try {
                stat.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}if(conn ! =null) {try {
                conn.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}}}Copy the code

Test.java

package com.jdbc.day0511.word;

import java.sql.*;
import java.util.Scanner;

Create table user(id,username,password); Enter the username and password on the keyboard. 2. Check whether the user logs in successfully. If the SQL has a query result, it succeeds; otherwise, it fails */
public class Test {
    public static void main(String[] args) {
        while(true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("Please enter user name:");
            String username = sc.nextLine();
            System.out.println("Please enter your password:");
            String password = sc.nextLine();

            boolean flag = login(username, password);
            if (flag) {
                System.out.println("Login successful!");
                break;
            } else {
                System.out.println("Login failed! Please check it carefully."); }}}private static Boolean login(String username,String password) {
        Boolean flag = null;
        Connection conn = null;
        PreparedStatement pstat = null;
        ResultSet rs = null;
        try {
            // Get the Connection database object Connection
            conn = JDBCUtils.getConnection();
            / / define the SQL
            String sql = "select * from user where username = ? and password = ?";
            // Get the Statement object for executing SQL statements
            // Prevent SQL injection, use prepareStatment
            pstat = conn.prepareStatement(sql);
            pstat.setString(1,username);
            pstat.setString(2,password);
            // Execute the SQL and receive the return results
            rs = pstat.executeQuery();
            // Check whether the query result has a value
            flag = rs.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstat,conn);
        }
        returnflag; }}Copy the code

1.2 Solutions

Delete the String that defines a variable in the static code block as follows: It conflicts with the variable defined in the static member variable

2. Cause analysis

  • Static variables and static code blocks are also executed in the same order as the code is written.

    • Static variables can be used in static blocks, but the static variables used must be declared before the static code block.
  • Static variables and static code blocks in Java are executed when the class is loaded. When an object is instantiated, the variables are declared and instantiated before the constructor is executed. If a subclass inherits from a parent class, the parent class’s static variables and static code blocks are executed first (declared first), followed by the subclass’s static variables and static code blocks. Again, parent and subclass variables (or non-static code blocks) and constructors are executed in sequence.

  • Instantiate object execution order:

    • Superclass static variable/static code block (first declared first executed)> Subclass static variable/static code block (first declared first executed)> Superclass variable/code block ((first declared first executed)> Superclass constructor > Subclass variable/code block ((first declared first executed)> Subclass constructor >