This article illustrates how Java loads the JDBC driver, and when you run this example code, the following statement will appear if the connection succeeds: SUN.JDBC.ODBC.JDBCODBCDRIVER@6EC12, if the connection is unsuccessful, An exception appears when loading the database driver.
Java load JDBC Implementation method:
You can explicitly load a driver by calling the Class.forName () method. The entry parameter for 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. When the database is connected, the Driver Manager (DriverManager) uses the loaded driver. The DriverManager Getdriver () method allows you to obtain driver information that has already been loaded. Program Code Requirements:
1. Write the basic framework of the Usedbdriver class, in which only the main () method is included, and the driver is loaded in the main () method.
2. The program code is as follows:
public class Usedbdriver
{public
static void Main (String arg[])
{
try
{
//Initialize and load JDBC-ODBC Driver
class.forname ("Sun.jdbc.odbc.JdbcOdbcDriver");
Enumeration Enum=drivermanager.getdrivers ();
Display driver information while
(Enum.hasmoreelements ())
{
System.out.println (enum.nextelement ());
}
Processing exceptions that may occur in loading the database
}
catch (java.lang.Exception exec)
{
System.out.println ("Exception Loading database Driver");
}
}
}
3. Because the JDBC class and the enumeration class are used in the program, the packages to be introduced are:
Import java.sql.*;
Import java.util.*;