Java load JDBC driver instance details

  • 2020-04-01 03:25:11
  • OfStack

This example is given to illustrate the method of load Java JDBC driver, running the example code, if the connection is successful will display a statement as follows: the sun, JDBC, odbc, 6 ec12 JdbcOdbcDriver @, if the connection is not successful, then the database driver display abnormal.

Java load JDBC implementation method:

You can explicitly load a driver by calling the class.forname () method. The entry parameter to this method is the driver to be loaded. For example: Class. Class.forname (" sun. JDBC. Odbc. JdbcOdbcDriver ") statement to load the sun company development of JDBC - odbc bridge. When connecting to a database, DriverManager USES this loaded driver. Information about the driver that has been loaded can be obtained by using DriverManager's getDriver() method. Program code requirements:

1. Write the basic framework for the useDBDriver class, which includes only 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();
//Displays driver information
while(enum.hasMoreElements())
{
System.out.println(enum.nextElement());
}
//Handles possible exceptions in the load database
}
catch(java.lang.Exception exec)
{
System.out.println(" An exception occurred in the load database driver ");
}
}
}

3. Because the program USES the JDBC class and the Enumeration class, the packages to be introduced are:


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

Related articles: