Java load JDBC Driver instance detailed _java

Source: Internet
Author: User
Tags driver manager odbc

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.*;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.