Establish a connection with the database
To establish a link with the DBMS, you must do the following: (1) load the driver (2) create a link
Load driver
Loading the driver is very simple and requires only one sentence of code. For example you, you want to drive with JDBC-ODBC bridge, then you write:
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Your driver document will tell you the class name. If your class name is jdbc. DriverXYZ, replace the Code with the following:
Class. forName ("jdbc. DriverXYZ ");
You do not need to create a driver instance and register it with DriverManager, because it is automatically loaded when you call class. forName. If you create an instance of your own, it will not be used or redundant, but it will not cause any harm to the program.
After you load the driver, you can establish a connection with the DBMS.
Create Link
The second step is to establish a connection with the DBMS. See the following code:
Connection con = DriverManager. getConection (Url, "myLogin", "myPassword ");
The second step is equally simple, and there is a little bit of difficulty about how that Url is provided, if you use the JDBC-ODBC bridge driver, then the jdbc url should be jdbc: odbc: next is the name of your data source or database. Therefore, if you use ODBC to access the ODBC data source named Fred, your jdbc url should be written as jdbc: odbc: Fred. In "myLogin", enter the user name for logging on to the DBMS. "myPassword" is a password. Look at the Code:
String url = "jdbc: odbc: Fred ";
Connection con = DriverManager. getConnection (url, "Fernanda", "J8 ");
If you want to use other drivers, the instructions will show you how to write a URL, carefully view the instructions, and some specific examples will show you how to do it.
Once the DriverManager. getConnection method finds the driver and source of the connection, it starts to establish a connection with the DBMS based on the user name and password provided later.