The example in this article illustrates how Java loads the JDBC driver. After running the example code in this article, if the connection is successful, the following statement will be displayed: sun.jdbc.odbc.JdbcOdbcDriver@6ec12. If the connection is unsuccessful, the message "Loading the database driver" will appear. abnormal.
Implementation method of loading JDBC in Java:
A driver can be loaded explicitly by calling the Class.forName() method. The entry parameter of this method is the driver to be loaded. For example: the Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") statement loads the JDBC-ODBC bridge developed by SUN Company. When connecting to the database, the driver manager (DriverManager) will use this loaded driver. The loaded driver information can be obtained through the getDriver() method of DriverManager. Program code requirements:
1. Write the basic framework of the useDBDriver class, which only includes the main() method, and loads the driver in the main() method.
2. The program code is as follows:
public class useDBDriver{public static void main(String arg[]){try{//Initialize and load the JDBC-ODBC driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Enumeration enum=DriverManager.getDrivers();//Display driver information while(enum.hasMoreElements()){System.out.println(enum.nextElement());}//Handle exceptions that may occur when loading the database}catch( java.lang.Exception exec){System.out.println("An exception occurred while loading the database driver");}}}3. Because the JDBC class and Enumeration class are used in the program, the packages to be introduced are:
import java.sql.*;import java.util.*;